2017-12-20 7 views
-1

私はアンドロイドアプリを作成するために以下のリンクを審査しました。Bluetooth低エネルギーアンドロイドアプリは常に動作していません

1)https://www.bignerdranch.com/blog/bluetooth-low-energy-part-1/

2)https://github.com/bignerdranch/android-bluetooth-testbed/tree/a/android-ble-part-1

しかし、アプリはすべての時間を働いていません。

+0

Bluetoothデバイスが接続されるブルートゥースの許可を追加する必要がありますが、データが受信されない広告を出してクライアント。 – Darshan

+0

投稿する前にヘルプセンターをお読みください。 –

答えて

0

あなたがBLEデバイス

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mHandler = new Handler(); 
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { 
     Toast.makeText(this, "BLE Not Supported", 
       Toast.LENGTH_SHORT).show(); 
     finish(); 
    } 
    final BluetoothManager bluetoothManager = 
      (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
    mBluetoothAdapter = bluetoothManager.getAdapter(); 

} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { 
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
    } else { 
     if (Build.VERSION.SDK_INT >= 21) { 
      mLEScanner = mBluetoothAdapter.getBluetoothLeScanner(); 
      settings = new ScanSettings.Builder() 
        .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) 
        .build(); 
      filters = new ArrayList<ScanFilter>(); 
     } 
     scanLeDevice(true); 
    } 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) { 
     scanLeDevice(false); 
    } 
} 

@Override 
protected void onDestroy() { 
    if (mGatt == null) { 
     return; 
    } 
    mGatt.close(); 
    mGatt = null; 
    super.onDestroy(); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_ENABLE_BT) { 
     if (resultCode == Activity.RESULT_CANCELED) { 
      //Bluetooth not enabled. 
      finish(); 
      return; 
     } 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

private void scanLeDevice(final boolean enable) { 
    if (enable) { 
     mHandler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       if (Build.VERSION.SDK_INT < 21) { 
        mBluetoothAdapter.stopLeScan(mLeScanCallback); 
       } else { 
        mLEScanner.stopScan(mScanCallback); 

       } 
      } 
     }, SCAN_PERIOD); 
     if (Build.VERSION.SDK_INT < 21) { 
      mBluetoothAdapter.startLeScan(mLeScanCallback); 
     } else { 
      mLEScanner.startScan(filters, settings, mScanCallback); 
     } 
    } else { 
     if (Build.VERSION.SDK_INT < 21) { 
      mBluetoothAdapter.stopLeScan(mLeScanCallback); 
     } else { 
      mLEScanner.stopScan(mScanCallback); 
     } 
    } 
} 


private ScanCallback mScanCallback = new ScanCallback() { 
    @Override 
    public void onScanResult(int callbackType, ScanResult result) { 
     Log.e("callbackType", String.valueOf(callbackType)); 
     Log.e("result", result.toString());//here you will get the details in ascii format and you have to convert that ascii to actual value 
     BluetoothDevice btDevice = result.getDevice(); 
     connectToDevice(btDevice); 
    } 

    @Override 
    public void onBatchScanResults(List<ScanResult> results) { 
     for (ScanResult sr : results) { 
      Log.e("ScanResult - Results", sr.toString()); 
     } 
    } 

    @Override 
    public void onScanFailed(int errorCode) { 
     Log.e("Scan Failed", "Error Code: " + errorCode); 
    } 
}; 

private BluetoothAdapter.LeScanCallback mLeScanCallback = 
     new BluetoothAdapter.LeScanCallback() { 
      @Override 
      public void onLeScan(final BluetoothDevice device, int rssi, 
           byte[] scanRecord) { 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Log.e("onLeScan", device.toString()); 
         connectToDevice(device); 
        } 
       }); 
      } 
     }; 

public void connectToDevice(BluetoothDevice device) { 
    if (mGatt == null) { 
     mGatt = device.connectGatt(this, false, gattCallback); 
     scanLeDevice(false);// will stop after first device detection 
    } 
} 

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { 
    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
     Log.e("onConnectionStateChange", "Status: " + status); 
     switch (newState) { 
      case BluetoothProfile.STATE_CONNECTED: 
       Log.e("gattCallback", "STATE_CONNECTED"); 
       gatt.discoverServices(); 
       break; 
      case BluetoothProfile.STATE_DISCONNECTED: 
       Log.e("gattCallback", "STATE_DISCONNECTED"); 
       break; 
      default: 
       Log.e("gattCallback", "STATE_OTHER"); 
     } 

    } 

    @Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
     List<BluetoothGattService> services = gatt.getServices(); 
     Log.e("onServicesDiscovered", services.toString()); 
     gatt.readCharacteristic(services.get(1).getCharacteristics().get 
       (0)); 
    } 

    @Override 
    public void onCharacteristicRead(BluetoothGatt gatt, 
            BluetoothGattCharacteristic 
              characteristic, int status) { 
     Log.e("onCharacteristicRead", characteristic.toString()); 
     gatt.disconnect(); 
    } 
}; 

からデータを取得するために、このサンプルコードを通過することができますし、

<uses-permission android:name="android.permission.BLUETOOTH"/> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
<usesfeature android:name="android.hardware.bluetooth_le"android:required="true"/> 
+0

私はコードにすべてのアクセス許可を追加しました。しかし、私のコードではデバイスは接続されていますが、サーバーデバイスから送信されたタイムスタンプはクライアントデバイスによって受信されません。 私のGitHubリポジトリを見つけてください:https://[email protected]/Darshan017/ble.git – Darshan

+0

onScanResult(int callbackType、ScanResult result)ではメッセージを取得します。結果からタイムスタンプを取得できます –

関連する問題