![](/img/trans.png)
[英]IOS:Write command values to corebluetooth peripheral device to get the response
[英]iOS:Send Command To peripheral and Receive response From it
我正在開發iOS中的核心藍牙應用程序。 我有智能手表設備,正在連接到我開發的應用程序,目的是能夠從其中發現服務和特征,當我嘗試從手表中讀取信息時,會出現以下錯誤
Bluetooth_iph[2688:614669] Did discover peripheral. peripheral: <CBPeripheral: 0x17daf4d0,
identifier = D675EA0B-1342-0C74-9D3D-98CAFA478985, name = BLEDEVICE, state = connecting>
rssi: -51, UUID: <CFUUID 0x17d96e80> D675EA0B-1342-0C74-9D3D-98CAFA478985
advertisementData: {
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = "BLEDEVICE";
kCBAdvDataServiceUUIDs = (
8880
);
kCBAdvDataTxPowerLevel = 7;
}
2014-10-17 10:06:13.695 Bluetooth_iph[2688:614669] WARNING: No service found
2014-10-17 10:06:18.353 Bluetooth_iph[2688:614669] Peripheral Connected
2014-10-17 10:06:18.354 Bluetooth_iph[2688:614669] started time is 10:06:10 17-10-14
2014-10-17 10:06:18.354 Bluetooth_iph[2688:614669] Scanning stopped
2014-10-17 10:06:18.485 Bluetooth_iph[2688:614669] Found a Device Manufacturer Name
2014-10-17 10:06:18.541 Bluetooth_iph[2688:614669] Error updating value for characteristic
8881 error: Reading is not permitted.
2014-10-17 10:06:18.601 Bluetooth_iph[2688:614669] Error updating value for characteristic
8881 error: Reading is not permitted.
2014-10-17 10:06:18.662 Bluetooth_iph[2688:614669] Error updating value for characteristic
8881 error: Reading is not permitted.
為了發現服務的特性,我編寫如下。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:
(CBService
*)service error:(NSError *)error
{
if (error)
{
NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error
localizedDescription]);
return;
}
if([service.UUID isEqual:[CBUUID UUIDWithString:@"8880"]])
{
for (CBCharacteristic * characteristic in service.characteristics)
{
/* Read manufacturer name */
if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"8881"]])
{
[_discoveredPeripheral readValueForCharacteristic:characteristic];
NSLog(@"Found a Device Manufacturer Name Characteristic - Read manufacturer
name");
}
}
}
if ( [service.UUID isEqual:[CBUUID UUIDWithString:CBUUIDGenericAccessProfileString]] )
{
for (CBCharacteristic *characteristic in service.characteristics)
{
/* Read device name */
if([characteristic.UUID isEqual:[CBUUID UUIDWithString:CBUUIDDeviceNameString]])
{
[_discoveredPeripheral readValueForCharacteristic:characteristic];
NSLog(@"Found a Device Name Characteristic - Read device name");
}
}
}
}
並為特征更新值
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:
(CBCharacteristic *)characteristic error:(NSError *)error
{
if (error)
{
NSLog(@"Error updating value for characteristic %@ error: %@", characteristic.UUID,
[error localizedDescription]);
return;
}
}
作為iOS開發的新目標,我的要求是我必須向ble設備發出一些請求,以便它能夠響應,並且應該在iOS應用程序中得到該響應。 以上代碼代表我的知識。 請通過代碼或其他示例幫助我。
您應該檢查CBCharacteristic
的properties
屬性-它會告訴您可以使用該特性做什么。 從此功能不支持您正在閱讀的消息-要么只能寫它,或者如果它是向中央發送信息,則可能必須使用通知或指示( CBCharacteristicPropertyNotify
和/或在properties
設置的CBCharacteristicPropertyIndicate
)。
如果是這種情況,則需要使用[peripheral setNotifyValue:YES forCharacteristic:characteristic];
當設備具有針對該特性的新數據時,它將調用peripheral:didUpdateNotificationStateForCharacteristic:error:
委托方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.