android – 以编程方式连接到蓝牙

我正在尝试以编程方式将我的设备连接到例如我的耳机…我有KitKat版本,并且所有工作都完美(蓝牙始终是自动连接而没有问题),但由于我已经更新到Lolipop它没有.我想知道是否有任何方法Android手机的任何配对设备连接到蓝牙,当它打开.

从现在起,我有了这个代码(获取设备名称和设备地址),因为我想到它可以连接做一些像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)上,如果我打开蓝牙,它将自动连接到设备(如果它靠近并配对),但在我的LG G3上,我必须去配置/蓝牙/配对设备,然后点击设备连接…我想避免这个…应该是可能的?

我想知道是否有可能连接到特定的蓝牙,只需添加设备名称或设备MAC …或多或少像Android,当我点击我的设备连接自动连接…我只是想获得CLICK活动.
我知道,Android应该自动连接到配对的设备,但有一些例外,不是…唯一的配对方式是做点击…这就是为什么我想知道是否有办法做到这一点.. .
我已经阅读并测试了kcoppock answer,但仍然没有工作.

任何建议?

编辑

我想要做的主要事情是自动连接我的蓝牙,但是自从我在Hey you上阅读后,我想到了,我知道这是一个Android的bug,所以我想做的是选择配对设备,然后单击要连接的设备(不做任何意图)并连接,而不是配置/蓝牙/ ….
Btw我已经阅读了StackOverflow上的任何答案,我发现使用套接字的东西是否用于连接蓝牙?可以解决这个问题吗?

解决方法

编辑回答最新的问题

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

我使用的目的是启用蓝牙,并使我的设备可以发现,然后我设置我的代码连接,并按一个按钮进行连接.在您的情况下,您还需要确保您的配件也可以发现.在我的情况下,我使用唯一的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

我已经在这里重现了一些代码,以防链接死机.

它基本上遵循与常规蓝牙连接相似的线路.您需要发现并查找支持的设备.
监控状态等
这是gatt的两个最相关的功能.

回调:

// 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

在这是擦您的设备是蓝牙智能还是智能准备?

链接提供了一个很好的智能设备列表.您还将了解实施程序的时间.

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

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...