コード例 #1
0
 public void toggleConnect(View view) {
   if (!mConnected) {
     mBluetoothLeService.connect(mDeviceAddress);
   } else {
     mBluetoothLeService.disconnect();
   }
 }
コード例 #2
0
 public void onResume() {
   if (!mConnected || mBluetoothLeService == null) return;
   ((Activity) context).registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
   if (mBluetoothLeService != null) {
     final boolean result = mBluetoothLeService.connect(mDeviceAddress);
     Log.d("registerReceiver", "Connect request result=" + result);
   }
 }
コード例 #3
0
 @Override
 protected void onResume() {
   super.onResume();
   registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
   if (mBluetoothLeService != null) {
     final boolean result = mBluetoothLeService.connect(mDeviceAddress);
     Log.d(TAG, "Connect request result=" + result);
   }
 }
コード例 #4
0
 @Override
 public void onServiceConnected(ComponentName componentName, IBinder service) {
   mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
   if (!mBluetoothLeService.initialize()) {
     Log.e("onServiceConnected", "Unable to initialize Bluetooth");
     ((Activity) context).finish();
   }
   // Automatically connects to the device upon successful start-up initialization.
   mBluetoothLeService.connect(mDeviceAddress);
 }
コード例 #5
0
 @Override
 public void onServiceConnected(ComponentName componentName, IBinder service) {
   mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
   if (!mBluetoothLeService.initialize()) {
     finish();
   }
   // Automatically connects to the device upon successful start-up
   // initialization.
   // 根据蓝牙地址,连接设备
   mBluetoothLeService.connect(mDeviceAddress);
 }
コード例 #6
0
 @Override
 protected void onResume() {
   super.onResume();
   // 绑定广播接收器
   registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
   if (mBluetoothLeService != null) {
     // 根据蓝牙地址,建立连接
     final boolean result = mBluetoothLeService.connect(mDeviceAddress);
   }
   log("onresume");
 }
コード例 #7
0
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
     case R.id.menu_connect:
       mBluetoothLeService.connect(mDeviceAddress);
       return true;
     case R.id.menu_disconnect:
       mBluetoothLeService.disconnect();
       return true;
     case android.R.id.home:
       onBackPressed();
       return true;
   }
   return super.onOptionsItemSelected(item);
 }
コード例 #8
0
  public void connect(String deviceAddress) {
    mDeviceAddress = deviceAddress;
    Intent gattServiceIntent = new Intent(context, BluetoothLeService.class);

    if (!((Activity) context)
        .bindService(
            gattServiceIntent, mServiceConnection, ((Activity) context).BIND_AUTO_CREATE)) {
      System.out.println("bindService failed!");
    }

    ((Activity) context).registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
    if (mBluetoothLeService != null) {
      final boolean result = mBluetoothLeService.connect(mDeviceAddress);
    } else {
      System.out.println("mBluetoothLeService = null");
    }
  }
コード例 #9
0
ファイル: MainActivity.java プロジェクト: Kneph/Ebandage
 @Override
 public void onServiceConnected(ComponentName name, IBinder service) {
   mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
   if (!mBluetoothLeService.initialize()) {
     Log.e("Bluetooth", "Unable to initialize Bluetooth");
     finish();
   }
   // get bluetooth device
   device = getBleDevice();
   if (device != null) {
     // Automatically connects to the device upon successful start-up initialization.
     boolean connection = mBluetoothLeService.connect(device.getAddress());
     if (connection) {
       // set treatment view
       setVisibility(TREATMENT_SERVICE_NOT_FOUND);
     }
   }
 }
コード例 #10
0
 @Override
 protected void onResume() {
   super.onResume();
   Intent intent = getIntent();
   mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
   mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);
   registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
   if (mBluetoothLeService != null) {
     final boolean result = mBluetoothLeService.connect(mDeviceAddress);
     Log.d(TAG, "Connect request result=" + result);
     if (result) {
       try {
         Thread.sleep(600L);
       } catch (InterruptedException e) {
         e.printStackTrace();
       }
       sendClearToSend();
     }
   }
 }