View unanswered posts | View active topics It is currently Thu Jun 22, 2017 6:36 pm



Reply to topic  [ 4 posts ] 
iPhone app to display sensor data using BLEShield 
Author Message

Joined: Fri Aug 03, 2012 11:41 pm
Posts: 1
Michael,

I am attempting to test a series of analog sensors with an iPhone 4s, using your BLEShield. I am writing the iPhone app in Xcode 4.4, using the CoreBluetooth framework, developing on a MacBook Pro running MacOS 10.7.4. I can verify that the hardware components are working correctly and I can connect to the BLEShield and explore the services via your BLExplr app. I am able to verify that the sensors are working correctly when using a physical USB connection and viewing the results in the serial monitor of the IDE, with the selected com port. But I am having difficulty figuring out how to connect to the BLEShield using the iPhone app to view the returned sensor data. For the iPhone app, I am currently working with sample apps included in the IOS SDK, and I am looking at examples found online. Working with sample code, I entered the UUID of the BLEShield, but all attempts to connect to the BLEShield have so far been unsuccessful. Can you please point me in the right direction? What do I need to do to sense the BLE device, connect to it, then read the connected analog sensor data? If you know of any sample code that can help, I would greatly appreciate it.

You had mentioned that you would provide an open sourced sample iPhone app. Is that available for download?

Thanks.

~Bart


Mon Aug 06, 2012 12:37 am
Profile
Site Admin

Joined: Thu Aug 02, 2012 11:36 am
Posts: 166
Hello Bart,

you need to discover to a peripheral which is advertising the BLE Shield Service (UUID F9266FD7-EF07-45D6-8EB6-BD74F13620F9)
See http://www.mkroll.mobi/?page_id=386 for a listing with what UUIDs you have to deal with.

Try to discover for peripherals which are advertising a service with UUID F9266FD7-EF07-45D6-8EB6-BD74F13620F9, connect to it and then try to get notifications on the RX characteristic with UUID 4585C102-7784-40B4-88E1-3CB5C4FD37A3.

I have currently no sample app available which is open source, but I will work on a BLE Shield App asap, which I will open source and illustrate how to connect to the BLE Shield.

There is a BLE Sample Application in the Apple iOS Developer Programme available which should be a good starting point for a simple BLE Shield app as well.
Have you tried this app?

Cheers,
Michael.


Mon Aug 06, 2012 8:05 pm
Profile WWW

Joined: Sun Aug 05, 2012 9:00 am
Posts: 5
Bart, here is some sample code that should get you started.

In the header file where you will run the following code, make sure it is a CBCentralManagerDelegate, and #import <CoreBluetooth/CoreBluetooth.h>


In viewDidLoad:

Code:
 mgr =
[[CBCentralManager alloc] initWithDelegate:self queue:nil];
[mgr scanForPeripheralsWithServices:nil options:nil];


Implement the following code to identify discovered BLE Devices. You will notice I have some code to identify Michael's Arduino BLE Shield, which is identified as "BLE Shield". I also created a mutable dictionary called "peripherals" which I can use to store peripherals that were found during the search, by their pUUID, so there will never be dupes.

Code:
- (void)centralManager:(CBCentralManager *)central
 didDiscoverPeripheral:(CBPeripheral *)peripheral
     advertisementData:(NSDictionary *)advertisementData
                  RSSI:(NSNumber *)RSSI {
   
    NSLog(@"Peripheral: %@ \n", peripheral);
   
    NSLog(@"pUUID: %@", [NSString stringWithCString:[self UUIDToString:[peripheral UUID]] encoding:NSStringEncodingConversionAllowLossy]);
    NSString *pUUID = [NSString stringWithCString:[self UUIDToString:[peripheral UUID]] encoding:NSStringEncodingConversionAllowLossy];
       
    [peripherals setObject:peripheral forKey:pUUID];
   
    NSLog(@"Identified Device: %@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]);
   
    if([[advertisementData objectForKey:@"kCBAdvDataLocalName"] isKindOfClass:[NSString class]]) {
        if([[advertisementData objectForKey:@"kCBAdvDataLocalName"] isEqualToString:@"BLE Shield"]) {
            [self connectPeripheral:peripheral];
           
        }
   
    }

}


My connectPeripheral method uses the CentralManager connect method to connect.
Code:
- (void) connectPeripheral:(CBPeripheral *)peripheral {
    NSLog(@"Connecting to peripheral with UUID : %@\r\n",[NSString stringWithCString:[self UUIDToString:peripheral.UUID] encoding: NSStringEncodingConversionAllowLossy]);

    [mgr connectPeripheral:activePeripheral options:nil];

}


In the delegate method for connected peripherals, we search for available services:
Code:
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    printf("Connection to peripheral with UUID : %s successfull\r\n",[self UUIDToString:peripheral.UUID]);
   
    activePeripheral = peripheral;
    [activePeripheral discoverServices:nil];
}


Once a service is discovered, the delegate method for that is called. From there, we call discoverCharacteristics, which I have wrapped in a method I made called getAllCharacteristicsFromArduino:
Code:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
    if (!error) {
        printf("Services of peripheral with UUID : %s found\r\n",[self UUIDToString:peripheral.UUID]);
        [self getAllCharacteristicsFromArduino:peripheral];
    }
    else {
        printf("Service discovery was unsuccessfull !\r\n");
    }
}

-(void) getAllCharacteristicsFromArduino:(CBPeripheral *)p{
    for (int i=0; i < p.services.count; i++) {
        CBService *s = [p.services objectAtIndex:i];
        printf("Fetching characteristics for service with UUID : %s\r\n",[self CBUUIDToString:s.UUID]);
        [p discoverCharacteristics:nil forService:s];
    }
}


This will allow you to discover all the characteristics from the BLE Shield. That should get you started. Once you get that far, I can walk you through some more advanced stuff (i.e. reading/wrting to the shield)


Fri Aug 17, 2012 4:29 am
Profile

Joined: Wed Feb 26, 2014 8:50 pm
Posts: 3
** EDIT

I just modified the "didUpdateValueForCharacteristic" a bit:

Code:
uint8_t databuffer [[_allData length]];
[characteristic.value getBytes:databuffer length:[characteristic.value length]];
      
uint8_t i;
for(i=0; i<[characteristic.value length]; i++)

{
     NSString *hexData = [NSString stringWithFormat:@"%x", databuffer[i]];
     NSString *str = hexData;
     NSMutableString * newString = [NSMutableString string];
         
     NSArray *components = [str componentsSeparatedByString:@""];
     for ( NSString * component in components )
          {
          int value = 0;
           sscanf([component cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
           [newString appendFormat:@"%c", (char)value];
          }
           NSLog(@"Sensor Data: %@", newString);
}


** /EDIT


@rosenjon, Thank you so much for your time in posting this example for calling the characteristics.

I've successfully connected and can see all of the characteristics from our BLEBees via the FIO V3 as described by Dr. Kroll. I'm wondering if you could briefly describe how one might parse the data from the Arduino serial monitor and present it in the Xcode NSLog?

Arduino sketch:

Code:
void setup()   {
  Serial.begin(9600);
  Serial.print("BLEbee v1.0.0 Sketch setup...");
  Serial1.begin(9600);
  Serial1.println(" done.");
}
void loop() // run over and over
{
    for (int x = 0; x < 10000; x++){
    Serial.println(x);
    delay(500);

  if (Serial.available()) {
    int ch = Serial.read();
    Serial.println(ch);
    Serial1.write(ch);
  }
  }
  if (Serial1.available()) {
    Serial.write(Serial1.read());
  }
}


Thanks again!
Mike


Mon Mar 03, 2014 6:49 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software for PTF.