20 Aralık 2017 Çarşamba

BluetoothDevice Sınıfı

Giriş
Şu satırı dahil ederiz.
import android.bluetooth.BluetoothDevice;
ACTION_ACL_CONNECTED Alanı
Elimizde bir BroadcastReceiver olsun.
<receiver android:name=".MyBluetoothReceiver" >
  <intent-filter>
    <action android:name="android.bluetooth.device.action.ACL_CONNECTED"/>
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED"/>
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED"/>
  </intent-filter>  
</receiver>  
Şöyle yaparız.
public class MyBluetoothReceiver extends BroadcastReceiver {
 @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    // When discovery finds a device
    if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {

      BluetoothDevice device = intent
                .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

    } else if (BluetoothAdapter.ACL_DISCONNECTED.equals(action)) {
      ...
    }
  }
}
createRfcommSocketToServiceRecord metodu
Şöyle yaparız.
BluetoothSocket btSocket = null;

UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

BluetoothDevice device = ...;

try {
  btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
  ...
}
getName metodu
Şöyle yaparız.
BluetoothDevice device = ...;
if(device.getName().equals("...")) {...}

Hiç yorum yok:

Yorum Gönder