我正在研究BLE设备连接(北欧设备)。 当我尝试连接到设备时,不会调用didUpdateValueForCharacteristic。在didDiscoverCharacteristicsForService中,我尝试执行" setNotifyValue",但通知仍然是NO。
扫描设备但未连接。
#import "BluetoothManager.h"
#import "Constants.h"
@implementation BluetoothManager
+ (id)sharedInstance
{
static BluetoothManager *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
-(id)init
{
if (self = [super init]) {
self.UART_Service_UUID = [CBUUID UUIDWithString:uartServiceUUIDString];
self.UART_TX_Characteristic_UUID = [CBUUID UUIDWithString:uartTXCharacteristicUUIDString];
self.UART_RX_Characteristic_UUID = [CBUUID UUIDWithString:uartRXCharacteristicUUIDString];
}
return self;
}
-(void)setUARTDelegate:(id<BluetoothManagerDelegate>)uartDelegate
{
self.uartDelegate = uartDelegate;
}
-(void)setLogDelegate:(id<BluetoothManagerDelegate>)logDelegate
{
self.logDelegate = logDelegate;
}
-(void)setBluetoothCentralManager:(CBCentralManager *)manager
{
if (manager) {
self.centralManager = manager;
self.centralManager.delegate = self;
}
}
-(void)connectDevice:(CBPeripheral *)peripheral
{
if (peripheral) {
self.bluetoothPeripheral = peripheral;
self.bluetoothPeripheral.delegate = self;
[self.centralManager connectPeripheral:peripheral options:nil];
}
}
-(void)disconnectDevice
{
if (self.bluetoothPeripheral) {
[self.centralManager cancelPeripheralConnection:self.bluetoothPeripheral];
}
}
-(void)writeRXValue:(NSString *)value
{
if (self.uartRXCharacteristic) {
NSLog(@"writing command: %@ to UART peripheral: %@",value,self.bluetoothPeripheral.name);
[self.bluetoothPeripheral writeValue:[value dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:self.uartRXCharacteristic type:CBCharacteristicWriteWithoutResponse];
}
}
#pragma mark - CentralManager delegates
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSLog(@"centralManagerDidUpdateState");
}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"didConnectPeripheral: %@", peripheral);
// [self.uartDelegate didDeviceConnected:peripheral.name];
[self.bluetoothPeripheral discoverServices:nil];
}
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"didDisconnectPeripheral");
// [self.uartDelegate didDeviceDisconnected];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CBPeripheralDisconnectNotification" object:self];
self.bluetoothPeripheral = nil;
}
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"didFailToConnectPeripheral");
// [self.uartDelegate didDeviceDisconnected];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CBPeripheralDisconnectNotification" object:self];
self.bluetoothPeripheral = nil;
}
#pragma mark Peripheral delegate methods
-(void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
NSLog(@"didDiscoverServices");
if (!error) {
NSLog(@"services discovered %lu",(unsigned long)[peripheral.services count] );
for (CBService *uartService in peripheral.services) {
NSLog(@"service discovered: %@",uartService.UUID);
if ([uartService.UUID isEqual:self.UART_Service_UUID])
{
NSLog(@"UART service found");
// [self.uartDelegate didDiscoverUARTService:uartService];
[self.bluetoothPeripheral discoverCharacteristics:nil forService:uartService];
}
}
} else {
NSLog(@"error in discovering services on device: %@",self.bluetoothPeripheral.name);
}
}
-(void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if (!error) {
if ([service.UUID isEqual:self.UART_Service_UUID]) {
for (CBCharacteristic *characteristic in service.characteristics)
{
if ([characteristic.UUID isEqual:self.UART_TX_Characteristic_UUID]) {
NSLog(@"UART TX characteritsic is found 1: %@", characteristic);
// [self.uartDelegate didDiscoverTXCharacteristic:characteristic];
[self.bluetoothPeripheral setNotifyValue:YES forCharacteristic:characteristic ];
NSLog(@"UART TX characteritsic is found 2: %@", characteristic);
}
else if ([characteristic.UUID isEqual:self.UART_RX_Characteristic_UUID]) {
NSLog(@"UART RX characteristic is found");
// [self.uartDelegate didDiscoverRXCharacteristic:characteristic];
self.uartRXCharacteristic = characteristic;
}
}
}
} else {
NSLog(@"error in discovering characteristic on device: %@",self.bluetoothPeripheral.name);
}
}
-(void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if (!error) {
NSLog(@"received update after Async: %@, UUID: %@",characteristic.value,characteristic.UUID);
if (characteristic.value.length != 0) {
// [self.uartDelegate didReceiveTXNotification:characteristic.value];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CBPeripheralTXNotification" object:self];
}
}
else {
NSLog(@"error in update UART value");
}
}
@end
这是日志。
2017-08-24 11:53:06.630020-0700 saygo_server[13043:6618152] Connected SAYCAR
2017-08-24 11:53:09.833453-0700 saygo_server[13043:6618152] didConnectPeripheral: <CBPeripheral: 0x1700f5480, identifier = D73B0603-3B87-4A1D-B7E4-D0E6BDEF5398, name = SAYCAR, state = connected>
2017-08-24 11:53:11.000994-0700 saygo_server[13043:6618152] didDiscoverServices
2017-08-24 11:53:11.729020-0700 saygo_server[13043:6618152] services discovered 1
2017-08-24 11:53:11.729508-0700 saygo_server[13043:6618152] service discovered: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
2017-08-24 11:53:11.729740-0700 saygo_server[13043:6618152] UART service found
2017-08-24 11:53:19.972727-0700 saygo_server[13043:6618152] UART TX characteritsic is found 1: <CBCharacteristic: 0x1700bf0e0, UUID = 6E400003-B5A3-F393-E0A9-E50E24DCCA9E, properties = 0x10, value = <fb>, notifying = NO>
2017-08-24 11:53:22.995069-0700 saygo_server[13043:6618152] UART TX characteritsic is found 2: <CBCharacteristic: 0x1700bf0e0, UUID = 6E400003-B5A3-F393-E0A9-E50E24DCCA9E, properties = 0x10, value = <fb>, notifying = NO>
2017-08-24 11:53:25.271358-0700 saygo_server[13043:6618152] UART RX characteristic is found
一切看起来都不错,但设备没有连接。有什么问题?
谢谢,