简体   繁体   English

如何通过 BLE(低功耗蓝牙)将 Android 设备连接到 iOS 设备

[英]How to connect Android device to an iOS device over BLE (Bluetooth Low Energy)

I'm trying to make an application which uses the new Bluetooth Low Energy API of Android.我正在尝试制作一个使用 Android新蓝牙低功耗 API的应用程序。 For this, I started with the BLE sample coming with API level 18 .为此,我从API 级别 18的 BLE 示例开始。

As I read that Android can not act as a Peripheral, I put the Android phone in central mode, scanning for BLE devices around it.当我读到 Android 不能充当外设时,我将 Android 手机置于中央模式,扫描它周围的BLE 设备 For this purpose, I made some testing with a Nordic Platform simulating a Heart Sensor.为此,我使用模拟心脏传感器的 Nordic 平台进行了一些测试。 Everything works in a perfect way!一切都以完美的方式运作!

After this, I try to pick an iPhone (iOS 7 beta 4) and put it in a Peripheral way and simulating a Heart Rate sensor as the previous testing.在此之后,我尝试选择iPhone(iOS 7 beta 4)并将其置于外设方式并模拟心率传感器作为之前的测试。 The Android app is able to see the device and connect to it. Android 应用程序能够看到设备并连接到它。 But after the connection is active, the 2 devices disconnect from each other in 3-4 seconds.但是在连接激活后,2 个设备会在 3-4 秒内彼此断开连接。 In addition to that, when I call discoverServices() on Android side, no callback is triggered!除此之外,当我在Android端调用discoverServices()时,没有触发回调! In some cases the Android device receives the "Connected" event even if iOS Bluetooth chip is Off.在某些情况下,即使 iOS 蓝牙芯片关闭,Android 设备也会收到“已连接”事件。 This is very strange.这很奇怪。 To prove that, I put the Nordic Board in Central mode and I was correctly able to connect to the iOS device with no problems.为了证明这一点,我将Nordic Board 置于 Central 模式,并且能够正确连接到 iOS 设备,没有出现任何问题。

What could it be?会是什么呢? There are some limitations on Android or iOS that don't permit to connect from an Android to an iOS or viceversa? Android 或 iOS 上有一些限制,不允许从 Android 连接到 iOS,反之亦然?

Thanks.谢谢。

EDIT: After some hard testing, I raised an issue on the AOSP page.编辑:经过一些艰苦的测试后,我在 AOSP 页面上提出了一个问题。 It can be checked here可以在这里检查

Adding a summary for reference:添加摘要以供参考:

What could it be?可能是什么? There are some limitations on Android or iOS that don't permit to connect from an Android to an iOS or viceversa? Android 或 iOS 上有一些限制,不允许从 Android 连接到 iOS,反之亦然?

When connecting to a GATT server that is advertised as dualmode (BLE and BR/EDR) device by calling connectGatt(...), the TRANSPORT_AUTO flag that is internally added makes Android to default to the BR/EDR mode ( link ).当通过调用 connectGatt(...) 连接到宣传为双模式(BLE 和 BR/EDR)设备的 GATT 服务器时,内部添加的 TRANSPORT_AUTO 标志使 Android 默认为 BR/EDR 模式(链接)。

Following workarounds are possible:以下解决方法是可能的:

  1. Peripheral side: Stop advertising BR/EDR capabilities by adjusting the appropriate flags ( link )外围端:通过调整适当的标志(链接)来停止广告 BR/EDR 功能
  2. Central side: Set the transport parameter explicitely to TRANSPORT_LE by calling the hidden version of connectGatt() using reflection中央端:通过使用反射调用connectGatt()的隐藏版本,将传输参数显式设置为TRANSPORT_LE

Example:例子:

public void connectToGatt(BluetoothDevice device) {    
   ...    
   Method m = device.getClass().getDeclaredMethod("connectGatt", Context.class, boolean.class, BluetoothGattCallback.class, int.class);    
   int transport = device.getClass().getDeclaredField("TRANSPORT_LE").getInt(null);     // LE = 2, BREDR = 1, AUTO = 0    
   BluetoothGatt mGatt = (BluetoothGatt) m.invoke(device, this, false, gattCallback, transport);    
   ... 
}

Edit 4/2016编辑 4/2016

As Arbel Israeli pointed out in the comment, Google introduced an overloaded version of connectGatt(...) which allows to specify the transport in Android M .正如Arbel Israel在评论中指出的那样,谷歌引入了connectGatt(...)的重载版本,它允许在Android M 中指定传输。

I've written a simple working example, well relatively simple, and included it open-source on Github: https://github.com/GitGarage .我写了一个简单的工作示例,相对简单,并将其开源在 Github 上: https : //github.com/GitGarage So far it has only been tested with an Android Nexus 9 and an iPhone 5s, but I presume it would also work with a Nexus 6 and various iPhone types.到目前为止,它仅在 Android Nexus 9 和 iPhone 5s 上进行了测试,但我认为它也适用于 Nexus 6 和各种 iPhone 类型。 So far it is set up explicitly to communicate between one Android and one iPhone, but I presume it is tweakable to do much more.到目前为止,它被明确设置为在一台 Android 和一台 iPhone 之间进行通信,但我认为它可以做更多的调整。

Maybe a bit delayed, but perhaps your pain can be relieved slightly ;)也许有点延迟,但也许你的痛苦可以稍微缓解;)

We have been experimenting a lot with cross platform BLE connections (iOS<-> Android) and learned that there are still many incompatibilities and connection issues.我们已经对跨平台 BLE 连接(iOS<-> Android)进行了大量试验,并了解到仍然存在许多不兼容和连接问题。 Aside to the instability of Android you should also consider that still, as of today, not that many Android devices actually support the BLE Peripheral mode.除了 Android 的不稳定性之外,您还应该考虑到,截至今天,实际上支持 BLE 外设模式的 Android 设备并不多。

Therefore, if your use case is feature driven and you only need basic data exchange I would suggest to look at Frameworks and Libraries that can achieve cross platform communication for you, without you needing to build it up from scratch.因此,如果您的用例是功能驱动的并且您只需要基本的数据交换,我建议您查看可以为您实现跨平台通信的框架和库,而无需您从头开始构建它。

For example: http://p2pkit.io or google nearby例如: http : //p2pkit.io或 google 附近

Disclaimer: I work for Uepaa, developing p2pkit.io for Android and iOS.免责声明:我为 Uepaa 工作,为 Android 和 iOS 开发 p2pkit.io。

You can now pass in TRANSPORT_LE via BluetoothDevice.connectGatt as of API 23.从 API 23 开始,您现在可以通过BluetoothDevice.connectGatt传入TRANSPORT_LE

Please see Android Documentation references below:请参阅下面的 Android 文档参考:

iOS设备总是外围设备或中心设备,但Android设备不能很少。在这种情况下,您的iOS设备必须是外围设备,而android设备必须是中心设备。我们可以认为外围设备是服务器,中心设备是客户端。这很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM