蓝牙连接

2016-04-14  本文已影响236人  coderTian

1.蓝牙搜索

[self.bluetooth startScanDevicesWithInterval:5 CompleteBlock:^(NSArray *devices) {
        //做的相应的操作
}];

2.蓝牙连接

[self.bluetooth connectionWithDeviceUUID:[((CBPeripheral *)self.dataSource[indexPath.row]).identifier UUIDString] TimeOut:3 CompleteBlock:^(CBPeripheral *device, NSError *err) {
        if (device) {
            NSLog(@"查找设备的服务和特征...");
            //注册对应的收发数据的通知
        }else{
            NSLog(@"连接设备失败");
        }
    }];

3.注册收发数据的通知

[self.bluetooth setNotificationForCharacteristicWithServiceUUID:@"1C85D7B7-17FA-4362-82CF-85DD0B76A9A5" CharacteristicUUID:@"7D887E40-95DE-40D6-9AA0-36EDE2BAE253" enable:YES];

**##4.发送数据

if (_bluetooth.isConnection) {
        [_bluetooth writeCharacteristicWithServiceUUID:@"1C85D7B7-17FA-4362-82CF-85DD0B76A9A5" CharacteristicUUID:@"7E887E40-95DE-40D6-9AA0-36EDE2BAE253" data:data];
    }

5.接收数据

-(void)ValueChange:(NSNotification *)noti{
    NSString *string=[[NSString alloc] initWithData:noti.object encoding:NSUTF8StringEncoding;
}

下载Demo--> 我的github地址

直接使用pod

pod 'Bluetooth', '~> 1.0.0'

上一篇下一篇

猜你喜欢

热点阅读