-
You can search "TemperatureSensor" in the XCode Organizer documentation
And you need to restart the scanning function if the device is disconnected:
/** Once the disconnection happens, we need to clean up our local copy of the peripheral
*/
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"Peripheral Disconnected");
[discoveredPeripheral release];
discoveredPeripheral = nil;
// We're disconnected, so start scanning again
[self scan];
} -
-
Since the CoreBluetooth framework for the iOS is not really stable, sometimes it will spend about 4s to connect the device.
It should be according to the bluetooth device that you want to connect.
If it is the other external bluetooth hardware, it should be connected within 1s.
If you use back the other iOS device and waiting for the connection, it will take much longer time or fail to connect.
So I suggest you to implement the this delegate:
/** If the connection fails for whatever reason, we need to deal with it.
*/
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"Failed to connect to %@. (%@)", peripheral, [error localizedDescription]);
[self cleanup];
}
But sometime, the CoreBluetooth will become hang in status while it try to connect to the iOS device.
And it may be fixed by restarting the bluetooth in the iOS device or you need to restart your iOS device.
It seems that we need to wait for Apple to fix this issue. -
-
for the -(void)cleanup function:
/** Call this when things either go wrong, or you're done with the connection.
* This cancels any subscriptions if there are any, or straight disconnects if not.
* (didUpdateNotificationStateForCharacteristic will cancel the connection if a subscription is involved)
*/
- (void)cleanup
{
// Don't do anything if we're not connected
if (!discoveredPeripheral.isConnected) {
return;
}
// See if we are subscribed to a characteristic on the peripheral
if (discoveredPeripheral.services != nil) {
for (CBService *service in discoveredPeripheral.services) {
if (service.characteristics != nil) {
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:myCharacteristicUUID]]) {
if (characteristic.isNotifying) {
// It is notifying, so unsubscribe
[discoveredPeripheral setNotifyValue:NO forCharacteristic:characteristic];
// And we're done.
return;
}
}
}
}
}
}
// If we've got this far, we're connected, but we're not subscribed, so we just disconnect
[centralManager cancelPeripheralConnection:discoveredPeripheral];
} -
-
First of All, in the .h, you should implement the CoreBluetooth delegate
@interface BlueToothClient : NSObject < CBCentralManagerDelegate, CBPeripheralDelegate>
And then create the CBCentralManager instance in .m,
// Start up the CBCentralManager
centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
---------------Step 1---------------
/** Scan for peripherals - specifically for our service's 128bit CBUUID
*/
- (void)scan
{
[centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:myServicUUID]]
options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
}
---------------Step 2---------------
/** This callback comes whenever a peripheral that is advertising the TRANSFER_SERVICE_UUID is discovered.
* We check the RSSI, to make sure it's close enough that we're interested in it, and if it is,
* we start the connection process
*/
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
//NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
// Ok, it's in range - have we already seen it?
if (discoveredPeripheral != peripheral) {
// Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
discoveredPeripheral = peripheral;
[discoveredPeripheral retain];
// And connect
NSLog(@"Connecting to peripheral %@", peripheral);
[centralManager connectPeripheral:peripheral options:nil];
}
}
---------------Step 3---------------
/** We've connected to the peripheral, now we need to discover the services and characteristics to find the 'transfer' characteristic.
*/
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Peripheral Connected");
// Stop scanning
[centralManager stopScan];
NSLog(@"Scanning stopped");
// Make sure we get the discovery callbacks
peripheral.delegate = self;
// Search only for services that match our UUID
[peripheral discoverServices:@[[CBUUID UUIDWithString:myServicUUID]]];
}
---------------Step 4---------------
/** The Transfer Service was discovered
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if (error) {
NSLog(@"Error discovering services: %@", [error localizedDescription]);
[self cleanup];
return;
}
// Discover the characteristic we want...
// Loop through the newly filled peripheral.services array, just in case there's more than one.
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:myCharacteristicUUID]] forService:service];
}
}
---------------Step 5---------------
/** The Transfer characteristic was discovered.
* Once this has been found, we want to subscribe to it, which lets the peripheral know we want the data it contains
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
// Deal with errors (if any)
if (error) {
NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
[self cleanup];
return;
}
// Again, we loop through the array, just in case.
for (CBCharacteristic *characteristic in service.characteristics) {
// And check if it's the right one
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:myCharacteristicUUID]]) {
// If it is, subscribe to it
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
// Once this is complete, we just need to wait for the data to come in.
}
---------------Step 6---------------
/** This callback lets us know more data has arrived via notification on the characteristic
*/
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
printf("stringFromData %s\n", [stringFromData UTF8String]);
if (error) {
NSLog(@"Error didUpdateValueForCharacteristic: %@", [error localizedDescription]);
return;
}
// Log it
NSLog(@"Received: %@", stringFromData);
}
---------------Step 7---------------
//For status change handling
/** The peripheral letting us know whether our subscribe/unsubscribe happened or not
*/
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if (error) {
NSLog(@"Error changing notification state: %@", error.localizedDescription);
}
// Exit if it's not the transfer characteristic
if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:myCharacteristicUUID]]) {
return;
}
// Notification has started
if (characteristic.isNotifying) {
NSLog(@"Notification began on %@", characteristic);
}
// Notification has stopped
else {
// so disconnect from the peripheral
NSLog(@"Notification stopped on %@. Disconnecting", characteristic);
[centralManager cancelPeripheralConnection:peripheral];
}
}
/** Once the disconnection happens, we need to clean up our local copy of the peripheral
*/
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"Peripheral Disconnected");
[discoveredPeripheral release];
discoveredPeripheral = nil;
// We're disconnected, so start scanning again
[self scan];
} -
-
-
-
-
You can try to see the demo save and load function:
- (void) addSavedDevice:(CFUUIDRef) uuid
{
NSArray *storedDevices = [[NSUserDefaults standardUserDefaults] arrayForKey:@"StoredDevices"];
NSMutableArray *newDevices = nil;
CFStringRef uuidString = NULL;
if (![storedDevices isKindOfClass:[NSArray class]]) {
NSLog(@"Can't find/create an array to store the uuid");
return;
}
newDevices = [NSMutableArray arrayWithArray:storedDevices];
uuidString = CFUUIDCreateString(NULL, uuid);
if (uuidString) {
[newDevices addObject:(NSString*)uuidString];
CFRelease(uuidString);
}
/* Store */
[[NSUserDefaults standardUserDefaults] setObject:newDevices forKey:@"StoredDevices"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void) loadSavedDevices
{
NSArray *storedDevices = [[NSUserDefaults standardUserDefaults] arrayForKey:@"StoredDevices"];
if (![storedDevices isKindOfClass:[NSArray class]]) {
NSLog(@"No stored array to load");
return;
}
for (id deviceUUIDString in storedDevices) {
if (![deviceUUIDString isKindOfClass:[NSString class]])
continue;
CFUUIDRef uuid = CFUUIDCreateFromString(NULL, (CFStringRef)deviceUUIDString);
if (!uuid)
continue;
[centralManager retrievePeripherals:[NSArray arrayWithObject:(id)uuid]];
CFRelease(uuid);
}
}
And the delegate function for the Retrieve Peripheral
- (void) centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals
{
CBPeripheral *peripheral;
/* Add to list. */
for (peripheral in peripherals) {
[central connectPeripheral:peripheral options:nil];
}
}
- (void) centralManager:(CBCentralManager *)central didRetrievePeripheral:(CBPeripheral *)peripheral
{
[central connectPeripheral:peripheral options:nil];
} -
-
First of all, if you want to use this delegate "- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI" in background, you need to follow two step:
Step 1: Add the key "Required background modes" in the info plist with value "App communicates using CoreBluetooth"
Step 2: You can only scan the bluetooth 4.0 device with the specific service in background. You are not allowed to scan any of the bluetooth 4.0 device in background. This means that you cannot pass nil in the service array of the scanning method. You need to pass the specific service array, e.g.
[centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"180d"]] options:options]; //Scan for the any bluetooth 4.0 device with "Heart beat service" support -
-
-
有個問題想請教一下 ,我現在掃描外設的UUID 爲什麼掃描不到呢?
我是這麼寫的
[_centralManger scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@“0xFFE0”] ] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES, CBCentralManagerOptionRestoreIdentifierKey :@YES }];
現在奇怪的是我想要連接的外設的SeverUUID確實是0xFFE0,但是我掃描這個UUID的時候就是掃描不到,
當我[_centralManger scanForPeripheralsWithServices:nil options:nil];這麼寫的時候就能掃描到外設,連接成功後打印出來的SeverUUID也是0xFFE0 -
I think you service uuid should be [CBUUID UUIDWithString:@"ffe0"] instead of [CBUUID UUIDWithString:@"0xFFE0"].
The service UUID should be a string and 0xFFE0 looks like a hex string.
or you can try this remove the option CBCentralManagerOptionRestoreIdentifierKey:
[centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"0xFFE0"] ] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES}]; -
-
My apps same as below code,but can`t get data from remote peripheral device using - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
Any comment and pointer will be welcome. Thanks.
;==============================================
// Start up the CBCentralManager
centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
---------------Step 1---------------
/** Scan for peripherals - specifically for our service's 128bit CBUUID
*/
- (void)scan
{
[centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:myServicUUID]]
options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
}
---------------Step 2---------------
/** This callback comes whenever a peripheral that is advertising the TRANSFER_SERVICE_UUID is discovered.
* We check the RSSI, to make sure it's close enough that we're interested in it, and if it is,
* we start the connection process
*/
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
//NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
// Ok, it's in range - have we already seen it?
if (discoveredPeripheral != peripheral) {
// Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
discoveredPeripheral = peripheral;
[discoveredPeripheral retain];
// And connect
NSLog(@"Connecting to peripheral %@", peripheral);
[centralManager connectPeripheral:peripheral options:nil];
}
}
---------------Step 3---------------
/** We've connected to the peripheral, now we need to discover the services and characteristics to find the 'transfer' characteristic.
*/
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Peripheral Connected");
// Stop scanning
[centralManager stopScan];
NSLog(@"Scanning stopped");
// Make sure we get the discovery callbacks
peripheral.delegate = self;
// Search only for services that match our UUID
[peripheral discoverServices:@[[CBUUID UUIDWithString:myServicUUID]]];
}
---------------Step 4---------------
/** The Transfer Service was discovered
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if (error) {
NSLog(@"Error discovering services: %@", [error localizedDescription]);
[self cleanup];
return;
}
// Discover the characteristic we want...
// Loop through the newly filled peripheral.services array, just in case there's more than one.
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:myCharacteristicUUID]] forService:service];
}
}
---------------Step 5---------------
/** The Transfer characteristic was discovered.
* Once this has been found, we want to subscribe to it, which lets the peripheral know we want the data it contains
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
// Deal with errors (if any)
if (error) {
NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
[self cleanup];
return;
}
// Again, we loop through the array, just in case.
for (CBCharacteristic *characteristic in service.characteristics) {
// And check if it's the right one
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:myCharacteristicUUID]]) {
// If it is, subscribe to it
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
// Once this is complete, we just need to wait for the data to come in.
}
---------------Step 6---------------
/** This callback lets us know more data has arrived via notification on the characteristic
*/
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
printf("stringFromData %s\n", [stringFromData UTF8String]);
if (error) {
NSLog(@"Error didUpdateValueForCharacteristic: %@", [error localizedDescription]);
return;
}
// Log it
NSLog(@"Received: %@", stringFromData);
}
---------------Step 7---------------
//For status change handling
/** The peripheral letting us know whether our subscribe/unsubscribe happened or not
*/
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if (error) {
NSLog(@"Error changing notification state: %@", error.localizedDescription);
}
// Exit if it's not the transfer characteristic
if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:myCharacteristicUUID]]) {
return;
}
// Notification has started
if (characteristic.isNotifying) {
NSLog(@"Notification began on %@", characteristic);
}
// Notification has stopped
else {
// so disconnect from the peripheral
NSLog(@"Notification stopped on %@. Disconnecting", characteristic);
[centralManager cancelPeripheralConnection:peripheral];
}
}
/** Once the disconnection happens, we need to clean up our local copy of the peripheral
*/
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"Peripheral Disconnected");
[discoveredPeripheral release];
discoveredPeripheral = nil;
// We're disconnected, so start scanning again
[self scan];
} -
My peripheral device is OEM device and can working fine with OEM iOS apps, but my apps and light blue can`t get any data after paired, but if I open OEM apps and pair with it then closed OEM apps, and then open my apps or light blue, at this time my apps or lighteblue tools can update data by using [peripheral setNotifyValue:YES forCharacteristic:characteristic];
I don`t know that the OEM device used which encryption method then cause my apps can`t get data? Please give me a hand. Thanks.
CoreBluetooth學習
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
iOS 動畫 - 窗景篇(三·完結)
柯烂
2020-07-08 12:37:45
小輪子 ios 獲取語言
whitehack
2020-07-08 12:33:27
使用ASIHTTPRequest 網絡連接
路漫漫走
2020-07-08 11:46:10
UIWebView計算高度
路漫漫走
2020-07-08 11:46:10
PLSqliteDatabase
路漫漫走
2020-07-08 11:46:10
xcode6創建工程,引用SETextView報錯
stlwtr
2020-07-08 11:07:17
Xcode6 自動提示圖片 KSImageNamed 支持自定義 UIImageNamed(name),UIImageLoad(name, type)方法
stlwtr
2020-07-08 11:07:17
objective-c 中數據類型之六 數值類(NSValue)
stlwtr
2020-07-08 11:07:17
iOS開發之CocoaPods(objective-c第三方庫管理工具)
stlwtr
2020-07-08 11:07:17
iOS SceneDelegate
_sevenK
2020-07-08 10:59:26
UIImage 加載圖片及SDWebimage的簡單使用介紹
zgcrichard
2020-07-08 08:39:15
iOS CADisplayLink筆記
卵毛三曲半
2020-07-08 08:18:11
24小時熱門文章