蓝牙4.0 For IOS

转自:http://see.sl088.com/wiki/%E8%93%9D%E7%89%994.0_For_IOS


目录

 [隐藏] 
  • 1 发现
  • 2 调试用APP
  • 3 困扰
  • 4 NRF8002
  • 5 一些研究
    • 5.1 官方Demo
  • 6 CC2570官方源码
  • 7 经验
  • 8 IOS的蓝牙4.0支持
  • 9 IOS库支持
  • 10 官方文档说法
  • 11 其他研究
  • 12 见识
    • 12.1 [iOS][RCtank]在xcode的iOS虚拟机中对BLE(蓝牙4.0)进行调试_jianglurong_新浪博客 沿途见识[2]
    • 12.2 在xcode的ios模拟器里调试BLE_strongleg_新浪博客 沿途见识[3]
    • 12.3 Next generation Bluetooth 4.0 for iOS / Mac - Joris Kluivers 沿途见识[4]
    • 12.4 Is there a way to access Bluetooth 4.0 Low Energy devices in iOS? - Stack Overflow 沿途见识[5]
    • 12.5 MAKE | Bluetooth 4.0 from Arduino to iPhone: No Jailbreaking, No MFi? 沿途见识[6]
    • 12.6 谈谈几个月以来开发android蓝牙4.0 BLE低功耗应用的感受 - 流风,飘然的风 - 博客园 沿途见识[7]
    • 12.7 Find My Car Smart:首款支持iPhone4S的蓝牙4.0找车应用 | 雷锋网 沿途见识[8]
  • 13 引用信息

发现

  • didDiscoverPeripheral看起来处理了搜索蓝牙设备的任务
  • 这是IOS和MAC10.7 以后的一个革新-不再需要MIFI认证或者耳机孔插入了,对蓝牙4.0的完整支持让访问外部设备更加简单。
  • 支持需要至少IOS 5.1以后的版本。
  • 有趣的是在模拟器中可以开启模拟器后插入蓝牙-是的完全没问题。
  • 支持的设备最少是iphone 4s:S
 [展开] 

一些文档图片

调试用APP

BLE Scanner
BLEXPlr

困扰

  • 当连接成功,已有绑定信息了,却丢失了
此时如果APP关闭,那是没事的,但是去设置里拨动一下蓝牙设置(开关一次)
匹配信息就丢失了

NRF8002

一些研究

  • 关于连接-NRF8002 连接

官方Demo

NRFready iOS Proximity App

CC2570官方源码

  • 看起来读取一个属性就是读取一个udid-最终的
-(void) readBattery:(CBPeripheral *)p {
    [self readValue:TI_KEYFOB_BATT_SERVICE_UUID characteristicUUID:TI_KEYFOB_LEVEL_SERVICE_UUID p:p];
}
 
#define TI_KEYFOB_BATT_SERVICE_UUID                         0x180F
 
-(void) readValue: (int)serviceUUID characteristicUUID:(int)characteristicUUID p:(CBPeripheral *)p {
    UInt16 s = [self swap:serviceUUID];
    UInt16 c = [self swap:characteristicUUID];
    NSData *sd = [[NSData alloc] initWithBytes:(char *)&s length:2];
    NSData *cd = [[NSData alloc] initWithBytes:(char *)&c length:2];
    CBUUID *su = [CBUUID UUIDWithData:sd];
    CBUUID *cu = [CBUUID UUIDWithData:cd];
    CBService *service = [self findServiceFromUUID:su p:p];
    if (!service) {
        printf("Could not find service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:su],[self UUIDToString:p.UUID]);
        return;
    }
    CBCharacteristic *characteristic = [self findCharacteristicFromUUID:cu service:service];
    if (!characteristic) {
        printf("Could not find characteristic with UUID %s on service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:cu],[self CBUUIDToString:su],[self UUIDToString:p.UUID]);
        return;
    }  
    [p readValueForCharacteristic:characteristic];
}

经验

  • 在插入蓝牙前先设置主设备,否则会被占用,看起来这里和设置蓝牙有关系
 [展开] 

提示被占用

user$ sudo nvram bluetoothHostControllerSwitchBehavior="never"
Password:********
  • 设置的含义看起来是设置HCI处理的默认行为

The following is a listing of behavior settings with respect to the OS X built-in Bluetooth driver. Refer to Listing 1 for setting the bluetoothHostControllerSwitchBehavior NVRAM variable.


bluetoothHostControllerSwitchBehavior="never" // when a new HCI is connected, the built-in driver stays attached to the built-in HCI
bluetoothHostControllerSwitchBehavior="always" // when a new HCI is connected, the built-in driver disconnects from the built in HCI and attaches to the external HCI
bluetoothHostControllerSwitchBehavior="default" // when a new HCI is connected, the built-in driver only disconnects from the built in HCI and attaches to the external HCI if the new module is not an Apple module.

  • 确保在插入前操作,这下应该就可以了,同时可以校验系统信息里是否是MAC自带的蓝牙了,看起来是蓝牙模拟器不能和系统共用一个-因为系统的要给鼠标啥的?
  • 如何确保蓝牙工作了呢?当没有外接蓝牙的时候,模拟器的蓝牙是无法开启的,一直菊花。

IOS的蓝牙4.0支持

  • 看起来自从5.1的一个beta版本后就开始支持了
iOS 5.1 beta 3
  • 那么5.1.1支持吗,看起来是的。

IOS库支持

CoreBluetooth_Framework
http://developer.apple.com/library/ios/#documentation/CoreBluetooth/Reference/CoreBluetooth_Framework/_index.html

官方文档说法

搭建蓝牙调试环境-官方文档地址
http://developer.apple.com/library/ios/#technotes/tn2295/_index.html
Requirements [1]
To test iOS Core Bluetooth applications in the iOS simulator environment, you must have the following
Mac system with Mac OS X 10.7.3 or greater
Xcode 4.2.1 with iOS 5 SDK or greater
Bluetooth LE USB adapter
For Mac systems with Bluetooth 4.0 built in, it is still a requirement to have a Bluetooth LE USB adapter in order to test iOS Core Bluetooth applications. An explanation as to why an adapter is required for this case is described in the section Bluetooth LE USB adapter Required on OS X Systems with Bluetooth 4.0 built-in

其他研究

蓝牙4.0 For IOS/IOS6的改进 蓝牙4.0 For IOS/WWDC 2012 演讲 蓝牙4.0 For IOS/工作模式

见识

[iOS][RCtank]在xcode的iOS虚拟机中对BLE(蓝牙4.0)进行调试_jianglurong_新浪博客 沿途见识[2]

1. 买一个CSR蓝牙4.0的usb dongle,这个网上到处都是,大约30多元。下面是图片,这几种都可以。该usb dongle在mac下是免驱动的,所以赠送的光盘没什么用处。


2.此时还无需插入usb dongle。打开OSX中的终端,敲入如下指令:
sudo nvram
bluetoothHostControllerSwitchBehavior="never"

3.进入OSX的系统信息,查看Hardware->Bluetooth设定,确认一下此时的Vendor ID is "0x5AC"即可,如下图所示。

4. 插入usb dongle,打开xcode,进行调试。

注意:调试时需要将iOS虚拟机中的蓝牙打开。如下图所示。

在xcode的ios模拟器里调试BLE_strongleg_新浪博客 沿途见识[3]

在iOS平台上开发BLE设备是一件很昂贵的工作,通常你需要准备以下东西:
较新的MacBook笔记本,运行xcode
iPhone 4S、新iPad或更新的iOS设备
最低99美元每年的开发者年费
但是如果你能接受只在模拟器上运行程序,而不需要真机调试或发布应用程序的话,后两项是可以省掉的。你需要准备的东西变成:
较新的MacBook笔记本,运行xcode 4.4、Mac OS X 10.7.3或更新版本
一个廉价的CSR蓝牙4.0 USB适配器(淘宝价大约30元RMB)
很奇怪的是,即使你的MacBook上带有蓝牙4.0,你还是需要这个CSR蓝牙4.0适配器。
具体步骤见苹果的文档:
http://developer.apple.com/library/ios/#technotes/tn2295/_index.html

这种调试方法在我同事的MacBook Air和我的CC2540节点板上调试通过。
还要感谢black在QQ群里共享了他修改过的TI iOS例程,使之可以点亮我的节点板上的LED,测试过程相当顺利!

Next generation Bluetooth 4.0 for iOS / Mac - Joris Kluivers 沿途见识[4]

As of iOS 5 and OS X 10.7 a new framework is available: The CoreBluetooth.framework. This framework enables developers to communicate with a new class of energy efficient Bluetooth devices.

The Bluetooth standard describes a number of use-cases called profiles. Each use-case standardizes one or more services a device can implement. A service publishes certain standardized characteristics which applications can read from or write to. So when writing code to interact with a heart rate service a developer can be sure it will work with both a Polar and a Wahoo strap. A full list of standardized Bluetooth low energy services is available on the Bluetooth developer site.
Using CoreBluetooth.framework

The Bluetooth standard describes a number of use-cases called profiles. Each use-case standardizes one or more services a device can implement. A service publishes certain standardized characteristics which applications can read from or write to. So when writing code to interact with a heart rate service a developer can be sure it will work with both a Polar and a Wahoo strap. A full list of standardized Bluetooth low energy services is available on the Bluetooth developer site.

When developing custom or hardware for something not covered by the standard it’s perfectly possible to define a new service and new characteristics.

None of these standardized profiles are currently implemented on iOS on a system wide level. It’s up to the developer to actually write client code to these services in the apps itself. The CoreBluetooth framework provides a general set of classes you can use to implement a profile.

Is there a way to access Bluetooth 4.0 Low Energy devices in iOS? - Stack Overflow 沿途见识[5]

As someone has mentioned, the APIs are available in the iOS 5.0 SDK. However, bear in mind that those APIs only provide support for ATT client, if you want to add services and act as a server you're out of luck for now. I truly hope Apple will add server support at some point, since some profiles (Alert Notification or Proximity) would benefit enormously: being able to be notified when you receive a new SMS/email or being able to "ping" your phone to find it would be great.

MAKE | Bluetooth 4.0 from Arduino to iPhone: No Jailbreaking, No MFi? 沿途见识[6]

We’ve long been able to get Arduino and Android to talk to each other over Bluetooth (for example, see the Android Controlled 3D Printed Slalombot), but doing that with an iOS device has required you to be enrolled in Apple’s MFi program or you to jailbreak your device.

That seems to have changed with the addition of Bluetooth 4.0′s “Bluetooth Smart Ready devices”. Alasdair Allan [via Tom Igoe] clued me into this really interesting development. Dr. Michael Kroll is working on a Bluetooth Low Energy (BLE) Shield for Arduino based on the Bluegiga BLE112 modules:

Right after my first contact with BLE technology I was thinking of a BLE Shield for the Arduino. I really liked the Arduino BT of the Bluetooth Shield from Seeed Studio, but since these boards are not “Made for iPod”, they are not connectable to iOS Devices. The idea I had in mind was to create something similar to the Seeedstudio Bluetooth Shield where the Arduino’s Serial RX/TX pins or Pins 2/3 (with SoftwareSerial) can be used to read data from and send data to an iOS device. That said, I started a first prototype of an Arduino Shield using Bluegiga’s BLE112 Bluetooth 4.0 single mode Development Kit. Once Bluegiga’s firmware was finished and my first sketch could read and write to my iPhone 4S, l then I created my first real Arduino Shield PCB.



Dr. Michael Kroll: Bluetooth Low Energy (BLE) Shield for Arduino

谈谈几个月以来开发android蓝牙4.0 BLE低功耗应用的感受 - 流风,飘然的风 - 博客园 沿途见识[7]

目前有蓝牙4.0功能的手机就只有几款,摩托罗拉的刀锋系列、HTC1x和三星GS3,HTC1x和三星GS3虽然有蓝牙4.0的硬件设备但迟迟没有成熟相关SDK可以进行开发,开源的博通BLE SDK开发出来的没有手机可以使用。
目前市场上销量最好的android蓝牙4.0的就是三星GS3,但系统本身的蓝牙功能连搜索BLE的设备都搜索不到,更别说连接使用了,持续关注了几个月也只有些零散的资料有些牛人整出了些framework但需要root的用户才能使用,跟苹果需要越狱一样,买GS3这种手机的人除了开发人员估计很多都不会root的,因为一root就失去了保修服务。
最近有看到了些希望,有人用三星的开发包弄出可以搜索到所有蓝牙设备的应用了,但没有真机测试还不能确定是否可以使用。
 

 
话说iPhone的版本也只有iPhone4S以上的手机才可以使用,但ios5升级到ios6系统后也是有验证bug的,导致手机app无法下发指令数据到蓝牙设备,所以需要交互的BLE应用都没法使用,iPhone5默认的系统是ios6应该也是用不了的,而且功耗比ios5的大很多,也只能坐等苹果升级系统,不过苹果由于闭源和开发投入比较大升级日期应该不会太久,应该等几个月就可以解决了。(后注:不出所料很快就出ios6.0.1版本解决了wifi连接等问题了,同时出ios6.1bate版本)
 

android的就蛋痛了,各个厂商的都定制成不一样的,各个系统版本上也有差异,兼容性很难保证,屏幕大小也差别非常大,开发广泛使用的应用在兼容性上就浪费了很多精力,而需要根据各个厂商提供的不同sdk开发的应用就只能适应某一类手机了,不同厂商的基本上都不兼容得重新开发了。

另外iPhone手机的蓝牙是不能够和android手机的蓝牙通信的,而且iPhone除了蓝牙4.0以外其他版本的蓝牙外设需要通过苹果的认证才能使用的,除非大厂商大批量生产否则不可能通过认证的。所以很多小公司,解决方案的公司做iPhone的应用只能做蓝牙4.0的,android的则没有这个限制。

 

 另外当初就搜索到健康类的应用有人做出产品的 但不是基于蓝牙4.0低功耗的 还是用蓝牙2.1的spp协议来做的,蓝牙2.1或者3.0的耗电是个比较大的问题,一些小外设,需要用纽扣电池的应用就无法真正使用,对一些创意产品如果需要经常换电池或充电对用户体验都会大打折扣,成本也会大大提高。

 相对wifi和zigbee无线方案,蓝牙和无线射频模块nRF24l01成本会低很多,wifi的好处是可以比较方便实现远程控制,距离比较远,信号也相对比较稳定。

 

补充内容:

经过不断努力终于可以让三星的支持BLE了,HTC1X,moto这几个主流蓝牙4.0的android都可以开发了,但估计以后只会先开发三星销量大的了,不同厂商底层差别太大了。

ios6.0的问题也解决了,解决办法暂时保密。 三星的解决办法之前在博客里公开过一段时间现在公司也要设为保密了。

另外ios6.0可以把iPhone手机当从设备了,可以两台iPhone通过蓝牙通信传数据了,有点类似spp协议,但android的蓝牙4.0的没有看到相关的api。

现在LG出的google nexus 4 也支持ble蓝牙4.0了,但目前也没有对应的api和sdk 

 ble的发展变化比较快,之前收藏的很多网址现在都访问不了了,更新或者作废了




补充内容2:

http://processors.wiki.ti.com/index.php/Category:BluetoothLE
这个是TI的wiki上的文章,上面有moto和HTC的例子程序的,但支持不全面,之前的描述是
MotoRAZRAndroidBLEDemo Obsolete. Updated Motorola BLE SDK with new API is not compatible.
HTC1X BLE API - Complete BLE API for HTC1X (European version only!) NEW!

现在有变成下面的了
MotoRAZRAndroidBLEDemo Obsolete. Updated Motorola BLE SDK with new API is not compatible.
HTC1X BLE API - Complete BLE API for HTC1X (European version only!) Obsolete. Updated Android SDK is not 100% compatible.

三星4.0.4可以搜索到ble设备,但连接有问题,需要升级到android4.1.1以后才行

 

Open Bluetooth Low Energy API
http://android-btle.github.com/framework/
这个是博通的开源的api 需要root权限才能用的,用户需要像精简版rom增加google maps的add-on那样增加相关文件到手机系统里面后才能用,除了需要root外对用户要求也比较高。

Find My Car Smart:首款支持iPhone4S的蓝牙4.0找车应用 | 雷锋网 沿途见识[8]

你需要安装一个小的USB蓝牙适配器在车上,当然是要能够跟iPhone配对的。这个适配器会跟你的iPhone上面的Find My Car Smart一起在后台运行。当你离开车的时候,它可以自动记住最后的GPS位置。无需手动调整,而且在后台运行对电池电量的耗费也不是很多。

引用信息

以下是[蓝牙4.0 For IOS]所有用到的引用信息,向这些伟大的家伙致敬:
  1. ^ http://developer.apple.com/library/ios/#technotes/tn2295/_index.html
  2. ^ http://blog.sina.com.cn/s/blog_60b2b1180101c9un.html
  3. ^ http://blog.sina.com.cn/s/blog_7cedb56d01016f81.html
  4. ^ http://joris.kluivers.nl/blog/2012/05/16/next-generation-bluetooth-for-ios-mac/
  5. ^ http://stackoverflow.com/questions/7852228/is-there-a-way-to-access-bluetooth-4-0-low-energy-devices-in-ios
  6. ^ http://blog.makezine.com/2012/03/19/bluetooth-4-0-from-arduino-to-iphone-no-jailbreaking-no-mfi/
  7. ^ http://www.cnblogs.com/zdz8207/archive/2012/10/17/bluetooth_ble_android.html
  8. ^ http://www.leiphone.com/find-my-car-smart.html

你可能感兴趣的:(蓝牙4.0 For IOS)