使用CoreBluetooth(BLE)将数据从CentralManager发送到PeripheralManager [英] Send Data from CentralManager to PeripheralManager using CoreBluetooth (BLE)

查看:143
本文介绍了使用CoreBluetooth(BLE)将数据从CentralManager发送到PeripheralManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在两台iOS设备(均具有6.1.4)上运行,其中一台设备充当中央设备,一台设备充当外围设备.

I have my app running on two iOS device (both have 6.1.4) where one device acts as the Central and one acts as a Peripheral.

我已经成功地将外围设备的通知(设置为通知的特征)发送到中央.

I have been successful in getting notifications (characteristics set up to notify) from the Peripheral over to the Central just fine.

但是,我想从中央到外围将一个值写入一个特定的可写特征,但这总是失败.

However, I was wanting to write a value from the Central to the Peripheral to a specific writable characteristic but that always fails.

在错误描述如下的地方,立即调用peripheral:didWriteValueForCharacteristic:error:代表的方法:

The peripheral:didWriteValueForCharacteristic:error: delegate's method is immediately called where the error description says:

一个或多个参数无效"

"One or more parameters were invalid"

我一直在网上寻找任何线索,但是还没有想出出什么问题.

I have been searching the net for any clues but have yet to come up with what is wrong.

这是我在外围设备上设置特征的方法:

This is how I setup the characteristic on the peripheral side:

[[CBMutableCharacteristic alloc] initWithType:<My CBUUID> properties:CBCharacteristicPropertyWrite value:nil permissions:0];

我看到,当我在中部发现特征时,就存在我的可写特征.我确实存储了一个引用,以后我在编写我的值时尝试使用它.我正在写的值只是一个文本字符串,我将其转换为如下所示的NSData:

I see that when I discover characteristics on the Central side that my writeable characteristic is there. I do store away a reference at that point that I later try to use when writing my value. The value that I am writing is just a text string that I convert to a NSData like so:

NSString *payloadMessage = @"Hello";
NSData *payload = [payloadMessage dataUsingEncoding:NSUTF8StringEncoding];
[peripheral writeValue:payload forCharacteristic:<myCharacteristic> type:CBCharacteristicWriteWithResponse];

我看到的唯一奇怪的是我的特征的属性设置为138.看起来它既可写又具有扩展的属性.不知道为什么CoreBluetooth框架会自动添加它.不知道这是否重要.

The only odd thing I can see is that my characteristic's properties is set to 138. It looks like it is both writeable as well as having extended properties. Not sure why that is automatically added by the CoreBluetooth framework. Don't know if it matters.

我试图发送的特征或数据似乎出了点问题,但对于我自己的一生,我无法弄清楚.如果外面有人有任何建议或想法,我将不胜感激

It seems like something is wrong with the characteristic or the data that I am trying to send but for the life of me I can not figure it out. If anyone out there has any suggestions or ideas I would appreciate the help

推荐答案

我确实在Bluetooh-dev列表中收到了解决我问题的答案.

I did receive an answer in the Bluetooh-dev list that solved my problem.

我没有使用正确的权限来创建我的特征.

I didn't create my characteristic with the right permissions.

这是我最初拥有的:

[[CBMutableCharacteristic alloc] initWithType:<CBUUID> properties:CBCharacteristicPropertyWrite value:nil permissions:0];

这应该是这样:

[[CBMutableCharacteristic alloc] initWithType:<CBUUID> properties:CBCharacteristicPropertyWrite value:nil permissions: **CBAttributePermissionsWriteable**];  

这篇关于使用CoreBluetooth(BLE)将数据从CentralManager发送到PeripheralManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆