Galaxy s6 Android LL:onConnectionStateChange给我返回133错误状态

时间:2015-09-06 13:19:03

标签: android bluetooth-lowenergy samsung-mobile android-bluetooth android-ble

我开发了一个应用程序,它必须重新连接到某个设备上安装的服务器应用程序。我已经在某些设备上测试了我的应用程序,但是在使用Lollipop的galaxy s6上我遇到了一些问题。

这是组合:

client (peripherall) is installed on galaxy tab 10.1 kitkat 
server (central role) is installed on galaxy s6 with lollipop 
ok

client (peripherall) is installed on galaxy nexus 6 lollipop
server (central role) is installed on galaxy s6 with lollipop 
ok

client (peripherall) is installed on galaxy s6 lollipop
server (central role) is installed on galaxy nexus 6 with lollipop 
not working

在最后一个组合上,连接功能返回133状态。

这是我的代码:

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    super.onConnectionStateChange(gatt, status, newState);
    Log.d("connect", "status = " + status + " newState = " + newState);
....

Kitkat和Lollipop有什么区别?我是否必须以不同的方式处理连接?

public void new_automatization() {
    automatizationInProgress = true;
    Log.d("connect","new auto: si parte");
    this.init();
    Bluetooth.getMe().stopScan();
    final String MACaddress=getDataFromPreferences("address");
    final String keycode=getDataFromPreferences("unlockcode");  

    if(!Bluetooth.getMe().isScanning()) {
        Bluetooth.getMe().startScan(new Bluetooth.ScanCallback() {
            boolean deviceIsFound = false;

            @Override
            public void onDeviceFound(final BluetoothDevice device, int rssi,byte[] scanRecord) {
                Log.d("testAlpha","dentro onDeviceFound");
                if(device != null) {
                    String address=new String();                    
                    if(device.getName() != null) {  
                        Log.d("testAlpha","trovato name: "+device.getName()+"mac: "+device.getAddress());
                        address=device.getName();
                        if(address.equals(MACaddress)) {
                            Log.d("testAlpha","mi connetto a: "+device.getName()+"mac: "+device.getAddress());
                            Bluetooth.getMe().stopScan();//possiamo riconnetterci
                            deviceIsFound=true;
                            setDevice(device);
                            unlock(keycode);
                            automatizationInProgress = false;
                        }
                    }
                } else {
                    Log.i("connect","dentro onDeviceFound null");
                    search_ended=true;
                    sendMessage(DEVICE_NOT_FOUND);
                    automatizationInProgress = false;
                }   
            }
        });
    }
}

这是startscan和stopscan功能:

    @SuppressWarnings("deprecation")
    public boolean startScan(ScanCallback callback, final long timeout) {

        stopScan();         
        scanCallback = callback;
        /*
          if (isScanning()) {
            stopScan();
            return true;
          }
          scanCallback = callback;

        // Stops scanning after a pre-defined scan period (10s).
        stopTimer = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(timeout);
                } catch (InterruptedException e) {
                    Log.i("debug","interrupted");
                    return;
                }

                Log.i("debug","bluetooth: inizia lo stopscan()");

                if(!isScanning())
                    return;

                Log.i("debug","amgbluetooth: pre scansione");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    BluetoothLeScanner scanner = bluetoothAdapter
                            .getBluetoothLeScanner();
                    scanner.stopScan(scanCallbackAfterLollipop);
                    scanning = false;
                } else {
                    bluetoothAdapter.stopLeScan(scanCallbackBeforeLollipop);
                    scanning = false;
                }
                Log.i("debug","amgbluetooth:stopTimer, invio null");

                if(!Thread.currentThread().isInterrupted())
                    scanCallback.onDeviceFound(null, 0, null);

            }
        });

        stopTimer.start();*/
        pippo.removeCallbacks(solPippo);
        pippo.postDelayed(solPippo, timeout);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Log.i("finalT","amgbluetooth: inizia lo startscan() LL");

            BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
            scanning = true;

            scanCallbackAfterLollipop = new ScanCallback() {
                @Override
                public void onScanResult(int callbackType, ScanResult result) {
                    super.onScanResult(callbackType, result);
                    Log.d("finalT", "Single Result: "+result.getDevice().getName());

                    processResult(result);
                    /*
                    scanCallback.onDeviceFound(result.getDevice(), result.getRssi(), result.getScanRecord().getBytes());*/
                }

                @Override
                public void onBatchScanResults(List<ScanResult> results) {
                    Log.d("finalT", "onBatchScanResults: "+results.size()+" results");
                    /*
                      for (ScanResult result : results) {
                          processResult(result);
                      }*/
                  }

                  @Override
                  public void onScanFailed(int errorCode) {
                      Log.w("finalT", "LE Scan Failed: "+errorCode);
                  }

                  private void processResult(ScanResult result) {
                        BluetoothDevice device = result.getDevice();
                        Log.i("finalT", "New LE Device: " + device.getName() + " @ " + result.getRssi());

                        scanCallback.onDeviceFound(result.getDevice(),
                                result.getRssi(), result.getScanRecord()
                                        .getBytes());

                        //stopScan();
                    }
                };

                ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).setReportDelay(0).build();
                scanner.flushPendingScanResults(scanCallbackAfterLollipop);
                scanner.startScan(null,settings,scanCallbackAfterLollipop);
                return true;
            } else {
                Log.i("finalT","amgbluetooth: inizia lo startscan() < LL");
                scanning = true;
                scanCallbackBeforeLollipop = new LeScanCallback() {

                @Override
                public void onLeScan(BluetoothDevice device, int rssi,
                        byte[] scanRecord) {

                    Log.i("finalT","device trovato: "+device.getName());
                    scanCallback.onDeviceFound(device, rssi, scanRecord);
                }
            };


            return bluetoothAdapter.startLeScan(scanCallbackBeforeLollipop);
        }
    }

    public void stopScan() {
        Log.i("debug1","stopScan()");
        if(isScanning()) {
            Log.i("debug","stopScan() inside if");
            //stopTimer.interrupt();
            pippo.removeCallbacks(solPippo);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
                scanner.stopScan(scanCallbackAfterLollipop);
                scanning = false;
            } else {
                bluetoothAdapter.stopLeScan(scanCallbackBeforeLollipop);
                scanning = false;
            }
        }
    }

并在unlock()我调用connect函数。

P.S。:我要连接的设备名称(nexus 6)位于macaddress变量中。

0 个答案:

没有答案