public void toggleConnect(View view) { if (!mConnected) { mBluetoothLeService.connect(mDeviceAddress); } else { mBluetoothLeService.disconnect(); } }
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); } }
@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); } }
@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); }
@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); }
@Override protected void onResume() { super.onResume(); // 绑定广播接收器 registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter()); if (mBluetoothLeService != null) { // 根据蓝牙地址,建立连接 final boolean result = mBluetoothLeService.connect(mDeviceAddress); } log("onresume"); }
@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); }
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"); } }
@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); } } }
@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(); } } }