蓝牙连接

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'

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容