相关疑难解决方法(0)

在Android上使用蓝牙的服务发现失败异常

我目前正在研究一种通过蓝牙连接到仪器的Android应用程序,需要编写字符串命令并接收字符串响应.目前我通过Wi-Fi进行TCP/IP连接/读/写工作,现在尝试实现蓝牙.但我遇到了一些障碍.我一直在网上搜索试图找到类似的东西的例子,并没有运气.我一直在使用Android开发人员资源示例:蓝牙聊天作为我的主要参考点.

我当前的代码似乎工作..然后它会在连接点抛出Service Discovery Failed异常.我正在使用DeviceListActivity该类来发现和选择我想要连接的设备.它返回anActivityResult,然后我的蓝牙类等待它处理它,然后连接到它.下面的代码几乎与蓝牙聊天应用程序相同.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(!m_BluetoothAdapter.isEnabled())
    {
        m_BluetoothAdapter.enable();
    }
    switch (requestCode) {
        case REQUEST_CONNECT_DEVICE:
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address
                String address = data.getExtras()
                                     .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                // Get the BLuetoothDevice object
                BluetoothDevice device = m_BluetoothAdapter.getRemoteDevice(address);
                // Attempt to connect to the device
                connect(device);
            }
            break;

        case REQUEST_ENABLE_BT:
            // When the request to enable Bluetooth returns
            if (resultCode …
Run Code Online (Sandbox Code Playgroud)

java android bluetooth

49
推荐指数
2
解决办法
6万
查看次数

28
推荐指数
3
解决办法
4万
查看次数

如何检测已发现和配对的设备是否可用?

我已经发现了其他设备,我已经配对了它.至少我把它放在Android手机上的配对设备列表中.

现在在BluetoothSocket.connect()上可能会出现两个问题:

  1. 远程设备已关闭或无法使用
  2. 远程设备忘记了配对,因为它只能配对另一台设备,并且已与另一部手机配对

    =>然后在一定的超时后连接失败.

是否可以检查已配对的设备是否真的可用,记住它是否与我的手机配对而没有连接到它?这与检测设备是否已连接无关.配对和可见与连接不同.

android bluetooth

11
推荐指数
1
解决办法
1万
查看次数

如何以编程方式检查Android中蓝牙设备的连接状态?

我可以枚举配对Bluetooth设备,我需要在应用启动时检查它们当前的连接状态.

但是,我发现无法获得连接状态?!

以下是我尝试但失败的一些努力:

  1. android.bluetooth.BluetoothDevice

它有获得远程设备的键状态的方法,和用于键状态可能值如下:BOND_NONE,BOND_BONDING,BOND_BONDED.但这不是连接状态.

  1. android.bluetooth.BluetoothProfile

该方法可用于获取连接状态,但BluetoothProfile必须首先获取对象.

public abstract int getConnectionState (BluetoothDevice device); 
Run Code Online (Sandbox Code Playgroud)

正如在doc中描述的那样:" 客户端应调用getProfileProxy(Context,BluetoothProfile.ServiceListener,int),以获取配置文件代理. ",它只能ServiceListener在状态发生变化时在被调用时进行检索.

如果我在启动时查询状态而没有发生任何状态更改,该怎么办?

  1. android.bluetooth.BluetoothManager

它提供了一种方法:

public int getConnectionState (BluetoothDevice device, int profile);
Run Code Online (Sandbox Code Playgroud)

但是它不能被用于检测连接状态的的Bluetooth扬声器,作为配置文件参数只接受GATTGATT_SERVER(用于低能量设备),其他轮廓(HEADSET,HEALTH,A2DP)不被支持.

  1. android.bluetooth.BluetoothAdapter

它提供了一种方法:

public int getProfileConnectionState (int profile);
Run Code Online (Sandbox Code Playgroud)

此功能可用于检查本地Bluetooth适配器是否连接到特定配置文件的任何远程设备.它可用于检查指定的配置文件.但它不能用于检查给定的设备.

有谁知道如何获得给定Bluetooth设备的连接状态?

提前致谢.

android bluetooth

10
推荐指数
1
解决办法
5425
查看次数

如何检查设备的蓝牙连接是否断开?

我想知道我与设备的蓝牙连接何时断开连接.我发现这个要检查:

    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter1);
    this.registerReceiver(mReceiver, filter2);
    this.registerReceiver(mReceiver, filter3);

  //The BroadcastReceiver that listens for bluetooth broadcasts
   mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
               //Device found
            }
            else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) {
               //Device is now connected
            }
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                //Done searching
            }
            else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
               //Device is about to …
Run Code Online (Sandbox Code Playgroud)

android android-bluetooth

5
推荐指数
1
解决办法
2万
查看次数