【问题标题】:Bluetooth connections on iOSiOS 上的蓝牙连接
【发布时间】:2015-08-03 14:37:49
【问题描述】:

我们正在尝试观察蓝牙设备何时与 iPhone 连接和断开连接。当某些感兴趣的设备连接时,我们本质上想要做出反应(不一定在前台)。在 Android 中,您可以接收 ACL_CONNECTED 和 ACL_DISCONNECTED 操作。

在 iOS 上与此行为等效的是什么?

我首先查看CoreBluetooth,然后发现它是用于蓝牙LE,然后查看ExternalAccessory,但它只显示the picker 中的MFI 设备,并且似乎需要用户通过选择器。我们发现唯一可行的方法是go through Apple's BluetoothManager itself,这是根据第 2.5 节中的AppStore guidelines 禁止的,大概是为了让他们可以完全控制 Apple 生态系统。


等效的 Android 代码:

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(BluetoothDevice.ACL_CONNECTED)) {
      //do stuff
    } else if (intent.getAction().equals(BluetoothDevice.ACL_DISCONNECTED)) {
      //do stuff
    }
  }

}

AndroidManifext.xml

<receiver
  android:name="com.myapp.MyReceiver"
  android:exported="true"
  android:enabled="true"
  android:label="MyReceiver">
  <intent-filters>
      <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
      <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
    </intent-filters>
</receiver>

【问题讨论】:

    标签: android ios bluetooth android-bluetooth ios-bluetooth


    【解决方案1】:

    我 published a demo app called Beetee 展示了蓝牙经典在 iOS 上的运行情况以及全球事件是什么。但是这个私有框架不是 AppStore 投诉的!

    【讨论】:

      【解决方案2】:

      你不能在 iOS 中直接监听像这样的全局事件,所有应用程序都在自己的沙箱中,彼此无权访问。

      也就是说,您可以使用CBCentralManager 的retrieveConnectedPeripheralsWithServices: 方法来获取当前连接的设备列表。定期调用该函数并检查更改可能会达到您想要的效果。

      【讨论】:

        猜你喜欢
        • 2014-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-09
        • 2016-09-13
        相关资源
        最近更新 更多