以编程方式连接到蓝牙

Connect to Bluetooth programmatically

我正在尝试以编程方式将我的设备连接到我的耳机上...我有 KitKat 版本并且一切正常(Bluetooth 总是自动连接而没有问题)但是因为我已经更新到 Lolipop 它没有。我想知道是否有任何方法可以在 Android phone 的任何配对设备打开时将其连接到 Bluetooth

从现在开始,我有了这段代码(获取设备名称和设备地址),因为我认为用它我可以连接做类似 device.connect(MAC-Address); 的事情,但它没有用...

    BluetoothAdapter bluetoothAdapter
    = BluetoothAdapter.getDefaultAdapter();
Set < BluetoothDevice > pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
    for (BluetoothDevice device: pairedDevices) {
        mDeviceName.add(device.getName());
        mDeviceMAC.add(device.getAddress());

    }
}
bluetoothClass.setDeviceName(mDeviceName);
bluetoothClass.setDeviceMac(mDeviceMAC);

问题

在我的 MotoG (KitKat) 上,如果我转动我的 Bluetooth,它会自动连接到设备(如果它靠近并配对...)但在我的 LG G3 上,我必须转到 Configuration/Bluetooth/Paired devices/ 然后点击设备进行连接...我想避免这种情况...应该可以吗?


我想知道是否有可能连接到特定的蓝牙只需添加 Device nameDevice MAC... 或多或少像 Android 当我点击时在我的设备上连接它会自动连接...我只想获取该 CLICK 事件。 我知道 Android 应该自动连接到配对的设备,但也有任何例外情况不会...配对它的唯一方法是点击...这就是为什么我想知道是否有办法做... 我已经阅读并测试了 kcoppock answer 但它仍然不起作用 ..

有什么建议吗?

编辑

我想做的主要事情是自动连接我的 Bluetooth,但自从我阅读了 的回答后...我想通了,我知道这是一个 Android 错误,所以我想做的是 select paired devices 然后单击我要连接的设备 (不做任何操作 Intent) 并连接它,而不是转到 Configuration/Bluetooth/...。 顺便说一句,我已经阅读了关于 Whosebug 的任何答案,我发现了一些带有 Sockets 的内容,它们是否用于连接 Bluetooth?这可能是一个解决方案吗?

编辑以回答最新问题

您可以避免使用 intent 来搜索配对设备。当连接到未配对的设备时,将弹出一条通知要求配对设备。配对后,这些设备不应再显示此消息,连接应该是自动的(根据您编写程序的方式)。

我使用 intent 启用蓝牙,并使我的设备可被发现,然后设置我的代码进行连接,然后按一个按钮进行连接。在您的情况下,您需要确保您的配件也可以被发现。在我的例子中,我使用了一个唯一的 UUID,并且两个设备都必须识别它才能连接。这只能在您对两个设备都进行编程时使用,无论是 android 还是一个 android 和另一种设备类型。

试试这个,看看它是否能解决您的问题。


这个答案是在被编辑为另一个问题之前对原始问题的回答。

为了清楚起见,我已经编辑了我的答案,因为我从评论中可以看出它具有误导性。你的问题有两个部分。

On my MotoG (KitKat) if I turn my Bluetooth it connects autommatically to device (if it's near and paired ofc...) but on my LG G3 I must go to Configuration/Bluetooth/Paired devices/ and there tap the device to connect... and I want to avoid this... should be possible?

这不是编程问题,而是平台问题。
在 Android 5.0 中有一个 有据可查的错误,蓝牙不会自动连接和许多其他 BT 问题。这些问题在 5.0 的所有更新中继续存在。版本,直到 5.1 才修复。升级。

http://www.digitaltrends.com/mobile/android-lollipop-problems/11/

http://forums.androidcentral.com/lg-g3/473064-bluetooth-streaming-choppy-lg-3-lollipop.html

第一站是更新到5.1

这些问题已在 Lollipop 更新 5.1 中得到解决

http://www.reddit.com/r/Android/comments/306m3y/lollipop_51_bluetooth/


编辑: 我不相信这会解决你的自动配对问题,你想知道如何使用 BTGatt。

I've seen if I type device. to check what can I do it let me connectGatt() means /.../ But I can't figure it out how to do this...

使用BluetoothGatt

https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html

This class provides Bluetooth GATT functionality to enable communication with Bluetooth Smart or Smart Ready devices. /.../ GATT capable devices can be discovered using the Bluetooth device discovery or BLE scan process.

https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback.html

这里有一个关于如何使用 BluetoothGatt 的很好的例子(它使用听力速率):
https://github.com/googlesamples/android-BluetoothLeGatt/blob/master/Application/src/main/java/com/example/android/bluetoothlegatt/BluetoothLeService.java

我在这里复制了一些代码,以防 link 死掉。

它基本上遵循与常规蓝牙连接类似的线路。您需要发现并找到支持的设备。 监控状态等
这是与关贸总协定最相关的两个特性。

回调:

// Implements callback methods for GATT events that the app cares about.  For example,
// connection change and services discovered.
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        String intentAction;
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            broadcastUpdate(intentAction);
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery:" +
                    mBluetoothGatt.discoverServices());

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            Log.i(TAG, "Disconnected from GATT server.");
            broadcastUpdate(intentAction);
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
    }
};

广播:

private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        if (data != null && data.length > 0) {
            final StringBuilder stringBuilder = new StringBuilder(data.length);
            for(byte byteChar : data)
                stringBuilder.append(String.format("%02X ", byteChar));
            intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
        }
    }
    sendBroadcast(intent);
}

这道题也有一些相关的代码,学习的时候可以帮助删减:
BLuetooth Gatt Callback not working with new API for Lollipop

问题来了。您的设备蓝牙智能或智能就绪了吗?

此 link 提供了一份很棒的智能设备列表。当你实施你的计划时,你也会发现。

http://www.bluetooth.com/Pages/Bluetooth-Smart-Devices-List.aspx

这就是我使用 Java 反射和 BluetoothProfile 完成这项工作的方式:

属性:

private boolean mIsConnect = true;
private BluetoothDevice mDevice;
private BluetoothA2dp mBluetoothA2DP;
private BluetoothHeadset mBluetoothHeadset;
private BluetoothHealth mBluetoothHealth;

致电:

mBluetoothAdapter.getProfileProxy(getApplicationContext() , mProfileListener, BluetoothProfile.A2DP);
mBluetoothAdapter.getProfileProxy(getApplicationContext() , mProfileListener, BluetoothProfile.HEADSET);
mBluetoothAdapter.getProfileProxy(getApplicationContext() , mProfileListener, BluetoothProfile.HEALTH);

听众:

private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (profile == BluetoothProfile.A2DP) {
                mBluetoothA2DP = (BluetoothA2dp) proxy;
                try {
                    if (mIsConnect) {
                        Method connect = BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class);
                        connect.setAccessible(true);
                        connect.invoke(mBluetoothA2DP, mDevice);
                    } else {
                        Method disconnect = BluetoothA2dp.class.getDeclaredMethod("disconnect", BluetoothDevice.class);
                        disconnect.setAccessible(true);
                        disconnect.invoke(mBluetoothA2DP, mDevice);
                    }
                }catch (Exception e){
                } finally {
                }
            } else if (profile == BluetoothProfile.HEADSET) {
                mBluetoothHeadset = (BluetoothHeadset) proxy;
                try {
                    if (mIsConnect) {
                        Method connect = BluetoothHeadset.class.getDeclaredMethod("connect", BluetoothDevice.class);
                        connect.setAccessible(true);
                        connect.invoke(mBluetoothHeadset, mDevice);
                    } else {
                        Method disconnect = BluetoothHeadset.class.getDeclaredMethod("disconnect", BluetoothDevice.class);
                        disconnect.setAccessible(true);
                        disconnect.invoke(mBluetoothHeadset, mDevice);
                    }
                }catch (Exception e){
                } finally {
                }
            } else if (profile == BluetoothProfile.HEALTH) {
                mBluetoothHealth = (BluetoothHealth) proxy;
                try {
                    if (mIsConnect) {
                        Method connect = BluetoothHealth.class.getDeclaredMethod("connect", BluetoothDevice.class);
                        connect.setAccessible(true);
                        connect.invoke(mBluetoothHealth, mDevice);
                    } else {
                        Method disconnect = BluetoothHealth.class.getDeclaredMethod("disconnect", BluetoothDevice.class);
                        disconnect.setAccessible(true);
                        disconnect.invoke(mBluetoothHealth, mDevice);
                    }
                }catch (Exception e){
                } finally {
                }
            }
        }
        public void onServiceDisconnected(int profile) {
        }
    };

我希望这对尝试连接蓝牙音频设备和耳机的任何人有所帮助。