现在有关物联网开发的蓝牙设备都是基于蓝牙4.0的
也就是#import <CoreBluetooth/CoreBluetooth.h>
这个框架
要进行蓝牙操作首先要实例化一个蓝牙管理者manager
manager= [[CBCentralManageralloc]init];
self.manager.delegate=self;//然后遵循代理
peripherals= [NSMutableArrayarray];//承接蓝牙外设
-(void)centralManagerDidUpdateState:(CBCentralManager*)central
{
if(central.state==CBCentralManagerStatePoweredOn)
{
[centralscanForPeripheralsWithServices:niloptions:nil];//扫描蓝牙
}
else
{
[SVProgressHUDsetStatus:@"对不起蓝牙不可用"];
}
}
-(void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber*)RSSI
{
if(![_peripheralscontainsObject:peripheral])
{
[_peripheralsaddObject:peripheral];//添加进数组
}
}
//连接蓝牙 因为要和安卓匹配而且硬件所以用蓝牙的名字来连接
-(void)BleLianjie:(NSString*)name
{
if(self.peripherals.count>0)
{
// NSLog(@"数组大于0");
for(CBPeripheral*peripheralinself.peripherals)
{
// NSLog(@"遍历数组%@",peripheral);
if([peripheral.nameisEqualToString:name])
{
// NSLog(@"赋值");
self.disper= peripheral;
//NSLog(@"链接");
[self.managerconnectPeripheral:peripheraloptions:nil];
}
}
}
else
{
// NSLog(@"数组小于0");
[_managerscanForPeripheralsWithServices:niloptions:nil];
}
}
-(void)centralManager:(CBCentralManager*)central didConnectPeripheral:(CBPeripheral*)peripheral
{
NSLog(@">>>连接到名称为(%@)的设备成功,原因",[peripheralname]);
self.disper= peripheral;
NSLog(@"_disper.state%ld",(long)self.disper.state);
self.disper.delegate=self;
[self.disperdiscoverServices:nil];
[selfstopCan];
if([_delegaterespondsToSelector:@selector(lockBLEFinish:)])
{
[_delegateperformSelector:@selector(lockBLEFinish:)withObject:@{@"result":peripheral,@"Tag":@(20)}];
}
}
-(void)centralManager:(CBCentralManager*)central didFailToConnectPeripheral:(CBPeripheral*)peripheral error:(NSError*)error
{
NSLog(@">>>连接到名称为(%@)的设备-失败,原因:%@",[peripheralname],[errorlocalizedDescription]);
[SVProgressHUDshowErrorWithStatus:@"连接失败"];
if([_delegaterespondsToSelector:@selector(lockBLEFinish:)])
{
[_delegateperformSelector:@selector(lockBLEFinish:)withObject:@{@"result":peripheral,@"Tag":@(21)}];
}
}
- (void)centralManager:(CBCentralManager*)central didDisconnectPeripheral:(CBPeripheral*)peripheral error:(NSError*)error{
[SVProgressHUDshowWithStatus:@"蓝牙断开连接"];
NSLog(@">>>外设连接断开连接%@: %@\n", [peripheralname], [errorlocalizedDescription]);
[SVProgressHUDdismiss];
if([_delegaterespondsToSelector:@selector(lockBLEFinish:)])
{
[_delegateperformSelector:@selector(lockBLEFinish:)withObject:@{@"result":peripheral,@"Tag":@(22)}];
}
}
//这个代理就是为了找到蓝牙设备的一个服务的
-(void)peripheral:(CBPeripheral*)peripheral didDiscoverServices:(NSError*)error
{
if(error)
{
NSLog(@">>>Discovered services for %@ with error: %@", peripheral.name, [errorlocalizedDescription]);
return;
}
//NSLog(@"Discovered services%@",peripheral.services);
for(CBService*serviceinperipheral.services)
{
if([service.UUID.UUIDStringisEqualToString:@"FF12"])
{
if(peripheral!=nil) {
[peripheraldiscoverCharacteristics:nilforService:service];
}
else
{
[selfcancleBle];
}
}
elseif([service.UUID.UUIDStringisEqualToString:@"180A"]||[service.UUID.UUIDStringisEqualToString:@"0000180A-0000-1000-8000-00805F9B34FB"])
{
if(peripheral !=nil) {
[peripheraldiscoverCharacteristics:nilforService:service];
}
else
{
[selfcancleBle];
}
}
elseif([service.UUID.UUIDStringisEqualToString:@"FF03"])
{
[peripheraldiscoverCharacteristics:nilforService:service];
}
elseif([service.UUID.UUIDStringisEqualToString:@"FFF0"])
{
[peripheraldiscoverCharacteristics:nilforService:service];
}
elseif([service.UUID.UUIDStringisEqualToString:@"0000FF00-0000-1000-8000-00805F9B34FB"])
{
[peripheraldiscoverCharacteristics:nilforService:service];
}
}
}
-(void)peripheral:(CBPeripheral*)peripheral didDiscoverCharacteristicsForService:(nonnullCBService*)service error:(nullableNSError*)error
{
if(error)
{
NSLog(@"error Discovered characteristics for %@ with error: %@", service.UUID, [errorlocalizedDescription]);
return;
}
//NSLog(@"peripheralperipheral%@",peripheral);
// NSLog(@"正在遍历所有的服务通道并进行操作%@",service.characteristics);
for(CBCharacteristic*characteristicinservice.characteristics)
{
if([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"FF01"]]||[characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"0000FF01-0000-1000-8000-00805F9B34FB"]])
{
if(self.writechar!= characteristic)
{
self.writechar= characteristic;
}
}
elseif([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"2A23"]])
{
if(_readChar!= characteristic) {
_readChar= characteristic;
}
if(peripheral !=nil) {
[peripheralreadValueForCharacteristic:_readChar];
}
else
{
[selfcancleBle];
}
}
elseif([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"FF02"]]||[characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"0000FF02-0000-1000-8000-00805F9B34FB"]])
{
if(_readChar!= characteristic) {
_readChar= characteristic;
}
if(peripheral !=nil)
{
[peripheralsetNotifyValue:YESforCharacteristic:_readChar];
}
else
{
[selfcancleBle];
}
}
}
}
//收数据的方法里面有我自己写的一些拼包组包的方法
-(void)peripheral:(CBPeripheral*)peripheral didUpdateValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error
{
// [NSThread sleepForTimeInterval:0.01];
NSLog(@"------characteristic.value---%@",characteristic.value);
if([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"FF02"]])
{_responseData= characteristic.value;
// NSLog(@"---------%@",characteristic.value);
if(characteristic.value.length>1)
{
NSMutableData*data_value = [[NSMutableDataalloc]initWithData:characteristic.value];
NSMutableData*dataValue_1 = [data_valuesubdataWithRange:NSMakeRange(1, data_value.length-1)];
Byteb0 = ((Byte*)([data_valuebytes]))[0];
if(b0==1)
{
_data_frist= [[NSMutableDataalloc]initWithData:dataValue_1];
NSMutableData*data_Length = [_data_fristsubdataWithRange:NSMakeRange(1,2)];
NSString*data_str = [selfconvertDataToHexStrBLE:data_Length];
stringLen=[NSStringstringWithFormat:@"%llu",strtoull([data_strUTF8String],0,16)];
NSIntegerint_length = [stringLenintegerValue];
if(int_length<16)
{
[selfChange_BLE_data:_data_frist];
}
}
else
{
if(_data_frist.length>1)
{
[_data_fristappendData:dataValue_1];
if(_data_frist.length>[stringLenintegerValue]+3)
{
[selfChange_BLE_data:_data_frist];
}
}
}
}
}
if([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"2A23"]])
{
NSString*value = [NSStringstringWithFormat:@"%@",characteristic.value];
NSMutableString*macString = [[NSMutableStringalloc]init];
[macStringappendString:[[valuesubstringWithRange:NSMakeRange(16,2)]uppercaseString]];
[macStringappendString:@":"];
[macStringappendString:[[valuesubstringWithRange:NSMakeRange(14,2)]uppercaseString]];
[macStringappendString:@":"];
[macStringappendString:[[valuesubstringWithRange:NSMakeRange(12,2)]uppercaseString]];
[macStringappendString:@":"];
[macStringappendString:[[valuesubstringWithRange:NSMakeRange(5,2)]uppercaseString]];
[macStringappendString:@":"];
[macStringappendString:[[valuesubstringWithRange:NSMakeRange(3,2)]uppercaseString]];
[macStringappendString:@":"];
[macStringappendString:[[valuesubstringWithRange:NSMakeRange(1,2)]uppercaseString]];
NSLog(@"MAC地址是macString:%@",macString);
_MacAddress= macString;
if([_delegaterespondsToSelector:@selector(lockBLEFinish:)])
{
[_delegateperformSelector:@selector(lockBLEFinish:)withObject:@{@"result":peripheral,@"Tag":@(23)}];
}
}
}