扫描后如何连接到Android上的Beacon?

扫描后如何连接到Android上的Beacon?,android,bluetooth-lowenergy,beacon,Android,Bluetooth Lowenergy,Beacon,我正在尝试连接到我扫描过的信标。 下面的代码是扫描可扩展设备的代码 private ScanCallback mScanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { processResult(result); } @Override

我正在尝试连接到我扫描过的信标。 下面的代码是扫描可扩展设备的代码

private ScanCallback mScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            processResult(result);
        }
        @Override
        public void onBatchScanResults(List<ScanResult> results){
            for (ScanResult result: results)
                processResult(result);
        }
        @Override
        public void onScanFailed(int ErrorCode){}
        private void processResult(final ScanResult result){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (result.getDevice().getName()!=null) {
                        String major = String.valueOf((result.getScanRecord().getBytes()[25]&0xff)*0x100
                                +(result.getScanRecord().getBytes()[26]&0xff));
                        String minor = String.valueOf((result.getScanRecord().getBytes()[27]&0xff)*0x100
                                +(result.getScanRecord().getBytes()[28]&0xff));


                            gpsTracker = new GpsTracker(MainActivity.this);
                            double latitude = gpsTracker.getLatitude();
                            double longitude = gpsTracker.getLongitude();
                            String address = getCurrentAddress(latitude, longitude);
                            double distance = getDistance(result.getRssi(),-69);



                            Log.d(TAG, "\nBluetooth Device Address : " + result.getDevice().getAddress() + "\n"
                                + "Bluetooth Device UUID : "+ UUID.nameUUIDFromBytes(result.getScanRecord().getBytes()).toString()
                                + "\nRSSI: " +result.getRssi()
                                + "\nScanResult bytes: " + result.getScanRecord().getServiceUuids()
                                + "\nResult: "+result
                                + "\nDistance: "+distance
                                    + "\nDevice Name Binary : "+result
                                    +" \nmajor = " +major
                                    +"\nminor = " +minor
                                    );

                    }
                }
            });
        }
    };
但是logcat上没有日志。所以我补充说

bluetoothGatt =result.getDevice().connectGatt(getApplicationContext(),true,gattCallback);

private final BluetoothGattCallback gattCallback =
            new BluetoothGattCallback() {
                @Override
                public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                    String intentAction;
                    if (newState == BluetoothProfile.STATE_CONNECTED){
                        intentAction = ACTION_GATT_CONNECTED;
                        connectionState = STATE_CONNECTED;
                        broadcastUpdate(intentAction);
                        Log.i(TAG, "Connected to GATT server");
                        Log.i(TAG, "Attempting to start service discovery " +
                                bluetoothGatt.discoverServices());
                    }else if(newState == BluetoothProfile.STATE_DISCONNECTED){
                        intentAction = ACTION_GATT_DISCONNECTED;
                        connectionState=STATE_DISCONNECTED;
                        Log.i(TAG, "disconnected from gatt");
                        broadcastUpdate(intentAction);
                    }
                }

                @Override
                public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                    if (status == BluetoothGatt.GATT_SUCCESS) {
                        Log.d(TAG, "GATT DISCOVERED");
                        Log.d(TAG, gatt.getServices().toString());
                        checkGattServices(gatt.getServices());
                        broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);

                    }
                    else
                        Log.w(TAG,"onSErviceDiscovered Receiveed" + status);
                }

                @Override
                public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                    Log.d(TAG, "onCharateristic Read");

                    if (status==BluetoothGatt.GATT_SUCCESS)
                        Log.d(TAG, "charateristic = " +characteristic.toString());
                        broadcastUpdate(ACTION_DATA_AVAILABLE,characteristic);
                }
            };
private int checkGattServices(List<BluetoothGattService> gattServices) {
        if (bluetoothAdapter == null || bluetoothGatt == null) {
            Log.d(TAG,"# BluetoothAdapter not initialized");
            return -1;
        }

        for (BluetoothGattService gattService : gattServices) {
            // Default service info
            Log.d(TAG,"# GATT Service: "+gattService.toString());


            // Extract characteristics
            List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                // Remember characteristic

                Log.d(TAG, "# GATT Char: "+gattCharacteristic.toString());

                }

        }

        return 0;
    }

像这样。我走的方向对吗?如果我是对的,我如何从中读取数据?

请具体说明与您的预期不同的情况。您提到“我正在尝试从scanresult连接到设备,尝试连接时遇到困难”,但显示的代码没有尝试连接。我添加了更多代码!感谢您的帮助如果您正在阅读Gatt特性,则您已连接到设备。由于GATT特性仅适用于连接。我建议您在扫描和回调时在IDE中设置断点,以查看代码在您的BLE设备中的流动情况。请具体说明与您的预期不同的情况。您提到“我正在尝试从scanresult连接到设备,尝试连接时遇到困难”,但显示的代码没有尝试连接。我添加了更多代码!感谢您的帮助如果您正在阅读Gatt特性,则您已连接到设备。由于GATT特性仅适用于连接。我建议您在扫描和回调时在IDE中设置断点,以查看代码在您的BLE设备存在时是如何流动的。
bluetoothGatt =result.getDevice().connectGatt(getApplicationContext(),true,gattCallback);

private final BluetoothGattCallback gattCallback =
            new BluetoothGattCallback() {
                @Override
                public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                    String intentAction;
                    if (newState == BluetoothProfile.STATE_CONNECTED){
                        intentAction = ACTION_GATT_CONNECTED;
                        connectionState = STATE_CONNECTED;
                        broadcastUpdate(intentAction);
                        Log.i(TAG, "Connected to GATT server");
                        Log.i(TAG, "Attempting to start service discovery " +
                                bluetoothGatt.discoverServices());
                    }else if(newState == BluetoothProfile.STATE_DISCONNECTED){
                        intentAction = ACTION_GATT_DISCONNECTED;
                        connectionState=STATE_DISCONNECTED;
                        Log.i(TAG, "disconnected from gatt");
                        broadcastUpdate(intentAction);
                    }
                }

                @Override
                public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                    if (status == BluetoothGatt.GATT_SUCCESS) {
                        Log.d(TAG, "GATT DISCOVERED");
                        Log.d(TAG, gatt.getServices().toString());
                        checkGattServices(gatt.getServices());
                        broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);

                    }
                    else
                        Log.w(TAG,"onSErviceDiscovered Receiveed" + status);
                }

                @Override
                public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                    Log.d(TAG, "onCharateristic Read");

                    if (status==BluetoothGatt.GATT_SUCCESS)
                        Log.d(TAG, "charateristic = " +characteristic.toString());
                        broadcastUpdate(ACTION_DATA_AVAILABLE,characteristic);
                }
            };
private int checkGattServices(List<BluetoothGattService> gattServices) {
        if (bluetoothAdapter == null || bluetoothGatt == null) {
            Log.d(TAG,"# BluetoothAdapter not initialized");
            return -1;
        }

        for (BluetoothGattService gattService : gattServices) {
            // Default service info
            Log.d(TAG,"# GATT Service: "+gattService.toString());


            // Extract characteristics
            List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                // Remember characteristic

                Log.d(TAG, "# GATT Char: "+gattCharacteristic.toString());

                }

        }

        return 0;
    }
D/BLE TEST: # GATT Service: android.bluetooth.BluetoothGattService@deca58c
    # GATT Char: android.bluetooth.BluetoothGattCharacteristic@7f502b7
D/BLE TEST: # GATT Char: android.bluetooth.BluetoothGattCharacteristic@ab01a24
D/BLE TEST: # GATT Char: android.bluetooth.BluetoothGattCharacteristic@ca498d
    # GATT Service: android.bluetooth.BluetoothGattService@e7b54d5
D/BLE TEST: # GATT Char: android.bluetooth.BluetoothGattCharacteristic@8987742
    # GATT Service: android.bluetooth.BluetoothGattService@1a34ea
D/BLE TEST: # GATT Char: android.bluetooth.BluetoothGattCharacteristic@c4d1353
    # GATT Service: android.bluetooth.BluetoothGattService@e4403db
    # GATT Char: android.bluetooth.BluetoothGattCharacteristic@de6b490
D/BLE TEST: # GATT Char: android.bluetooth.BluetoothGattCharacteristic@d9ef189
    # GATT Service: android.bluetooth.BluetoothGattService@2848e78
D/BLE TEST: # GATT Char: android.bluetooth.BluetoothGattCharacteristic@33ac58e
    # GATT Char: android.bluetooth.BluetoothGattCharacteristic@20b91af
    # GATT Service: android.bluetooth.BluetoothGattService@ea96951
D/BLE TEST: # GATT Char: android.bluetooth.BluetoothGattCharacteristic@1aa89bc
    # GATT Char: android.bluetooth.BluetoothGattCharacteristic@efc1d45
D/BLE TEST: # GATT Char: android.bluetooth.BluetoothGattCharacteristic@aa1ac9a
    # GATT Char: android.bluetooth.BluetoothGattCharacteristic@28999cb
    # GATT Service: android.bluetooth.BluetoothGattService@1deb5b6
D/BLE TEST: # GATT Char: android.bluetooth.BluetoothGattCharacteristic@f7485a8
    # GATT Char: android.bluetooth.BluetoothGattCharacteristic@d4648c1
    # GATT Char: android.bluetooth.BluetoothGattCharacteristic@75df866