连接到蓝牙编程蓝牙、连接到

2023-09-12 06:37:16 作者:穷得只剩下作业了

我想我的设备编程方式连接到例如在我的耳机......我奇巧版本和所有的工作完美(蓝牙始终是没有问题的连接autommatically),但因为我已经更新到棒棒糖事实并非如此。我想知道是否有任何的方式来连接我的 Android手机的所有配对设备蓝牙时,开启。

I'm trying to connect programmatically my device to for example on my Headsets... I had KitKat version and all worked perfect (Bluetooth always was connecting without problems autommatically) but since I've updated to Lolipop it doesn't. I'd like to know if there is any way to connect any paired device of my Android phone to Bluetooth when it turns on.

由于现在我已经这样code(获取设备名称和设备地址),因为我认为它我可以连接做一些像 device.connect(MAC地址); 但它没有工作...

Since now I've this code (gets the Device name and Device Address) because I thought with it I could connect doing something like device.connect(MAC-Address); but it didn't work...

    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(奇巧),如果我把我的蓝牙它autommatically连接设备(如果它的附近,配对OFC ...),但在我的LG G3我必须去配置/蓝牙/配对设备/有挖掘设备连接...我想避免这种情况...应该是可能的吗?

Question

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?

我想知道是否有任何可能连接到特定的蓝牙只需添加设备名称设备MAC ...更多或更少的如安卓并当我点击我的设备上进行连接它连接autommatically ......我只是想获得的Click事件。 我知道,安卓应autommatically连接到配对的设备,但有那并不是没有任何例外......只有这样,才能配对它做的点击...这就是为什么我敢知道是否有办法做到这一点? 我已阅读并测试 kcoppock答案但它仍然没有工作。

I would like to know if there is any possibility to connect to specific Bluetooth just adding the Device name or Device MAC... More or less like Android does when I click on my device to connect it connects autommatically... I just want to get that CLICK event. I know that Android should connect autommatically to a paired device but there's any exceptions that doesn not ... the only way to pair it it's doing the click... that's why I'm wondering if it's there a way to do it... I've read and tested kcoppock answer but it still don't work ..

任何建议?

这是我想要做的主要事情是连接我的蓝牙 autommatically但因为我读过上的嘿,你回答......我想通了,我知道这是一个安卓的bug,所以的事情,我想要做的只是选择配对设备键,然后单击我要连接的(没有做任何意图)并连接它,而不是去配置/蓝牙/...。 顺便说一句,我读计算器任何的答案,我发现了一些与插槽是他们用来连接蓝牙?可能是它的解决方案?

The main thing that I wanted to do is to connect my Bluetooth autommatically but since I've read on Hey you answer... I figured it out and I know it's an Android bug, so the thing that I would like to do is select the paired devices and then click on the device that I want to connect (Without doing any Intent) and connect it, instead to go Configuration/Bluetooth/.... Btw I've read any answers on StackOverflow and I found something with Sockets are they used to connect Bluetooth?Could be it a solution?

推荐答案

修改回答最新的问题

您可以避免使用意向来搜索配对设备。当连接到未配对的装置,通知会弹出询问将设备配对。一旦配对这条消息应该不会再为这些设备的显示,该连接应该是自动(根据你如何编写好程序)。

You can avoid using an intent to search for paired devices. When connecting to a device that is not paired, a notification will pop up asking to pair the devices. Once paired this message should not show again for these devices, the connection should be automatic (according to how you have written your program).

我使用的意图,启用蓝牙,并让我的设备被发现,然后我建立了我的code连接,和preSS一个按钮连接。在你的情况,你需要确保你的配件都发现也。在我来说,我使用一个唯一的UUID,并且两台设备都必须认识到这一点进行连接。如果正在编程两个设备这只能用于,是否都是机器人或者一个机器人和一个其他设备类型。

I use an intent to enable bluetooth, and to make my device discoverable, I then set up my code to connect, and press a button to connect. In your case, you will need to ensure your accessories are discoverable also. In my case I use a unique UUID, and both devices must recognise this to connect. This can only be used if you are programming both devices, whether both are android or one android and one other device type.

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

Try this, and see if it solves your problem.

这个答案原来的问题就被编辑是另外一个问题了。

我已经编辑我的清晰度的答案,我可以从它是具有误导性的评论见。你的问题有两个部分。

I've edited my answer for clarity as I can see from the comments it is misleading. Your question has two parts.

在我MotoG(奇巧),如果我把我的蓝牙它连接autommatically   到设备(如果它的附近,OFC配对......),但在我的LG G3我必须去   要配置/蓝牙/配对设备/有设备轻点   连接...我想避免这种情况...应该是可能的吗?

iOS蓝牙编程

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?

这是更小的编程问题的平台问题等等。 有一个有据可查的Andr​​oid 5.0 用蓝牙无法自动连接等诸多BT问题的bug。这些问题继续与5.0的所有更新。版本和不固定,直到5.1。升级。

This is less of a programming issue and more of a platform issue. There is a well documented bug in Android 5.0 with Bluetooth not automatically connecting and many other BT issues. These issues continue with all the updates on 5.0. versions and is not fixed until the 5.1. upgrade.

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

这些问题都在棒棒堂得到解决更新5.1

These issues have been addressed in the Lollipop update 5.1

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

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

如果我输入设备我见过。检查我能做些什么这让我   connectGatt()是指/.../   但我无法弄清楚如何做到这一点?

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

这个类提供了对蓝牙关贸总协定功能,以使   与蓝牙智能或智能Ready设备的通信。    /.../   GATT功能的设备可以使用蓝牙设备被发现   发现或BLE的扫描过程。

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

Here is a great example of how to use BluetoothGatt (it uses hear rate): https://github.com/googlesamples/android-BluetoothLeGatt/blob/master/Application/src/main/java/com/example/android/bluetoothlegatt/BluetoothLeService.java

我已经转载了一些code在这里,在情况下,链接死亡。

I have reproduced some of the code here, in case the link dies.

它基本上遵循类似的路线到正规的蓝牙连接。你需要去发现和寻找支持的设备。 监视状态等 这是两个最相关的功能,以关贸总协定。

It basically follows similar lines to a regular bluetooth connection. You need to discover and find supported devices. Monitor state, etc. These are the two most pertinent features to 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);
}

这问题也有一些相关的code,可以帮助剪下来学习的时候: BLuetooth盖特回调不工作与新的API的棒棒糖

This question also has some relevant code that may help cut it down when learning: BLuetooth Gatt Callback not working with new API for Lollipop

现在这里的难题。是你的设备的蓝牙智能或智能准备好了吗?

Now here's the rub. Are your devices bluetooth smart or smart ready?

这个链接给出了智能设备的大名单。您还可以发现当你执行你的计划。

This link gives a great list of smart devices. You will also find out when you implement your program.

http://www.bluetooth.com/Pages/Bluetooth-智能设备 - List.aspx