を学ぼうとすることは、私は私のデバイスのUUIDを見つけるためにデバッグを使用し、Bluetoothのソケットに接続し、RFCOMMの送信を開始する例です。必ずしも別のクラスは必要ありませんが、接続上で送信されるデータを探すために常に実行される別のスレッドを作成しました。
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothSocket mRf, ml2;
if (mBluetoothAdapter != null) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
if (mBluetoothAdapter.isEnabled()) {
BlueHelper client = new BlueHelper();
Af1000Computer = client.findDevice(mBluetoothAdapter);
if (Af1000Computer != null) {
try{
ParcelUuid[] uuids = Af1000Computer.getUuids();
mRf = Af1000Computer.createInsecureRfcommSocketToServiceRecord(UUID.fromString("0000110e-0000-1000-8000-00805f9b34fb"));
//mRf = Af1000Computer.crea
//ml2 = new BluetoothSocket(BluetoothSocket.TYPE_L2CAP, -1, true, true, this, 2, null);
try {
mRf.connect();
}catch (Exception e){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Error: " + e.getLocalizedMessage())
.setTitle("Error in creating connection!");
AlertDialog dialog = builder.create();
dialog.show();
}
if (mRf.isConnected()){
fab.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.greenaf)));
connected = true;
slow.setEnabled(true);
harvest.setEnabled(true);
fast.setEnabled(true);
reverse.setEnabled(true);
thread = new RfcommThread(mRf);
thread.run();
}
// RfcommThread thread = new RfcommThread(mBluetoothSocket);
//View v = findViewById(R.id.seekBar1);
//thread.run(1, 2, 3);
}catch (Exception e){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Error: " + e.getLocalizedMessage())
.setTitle("Error in creating comm thread!");
AlertDialog dialog = builder.create();
dialog.show();
}
}
if (Af1000Computer == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Cannot find the AF1000 Harvester device! \nHave you connected to the bluetooth device?")
.setTitle("Error in Pairing");
AlertDialog dialog = builder.create();
dialog.show();
}
}
}
if (mBluetoothAdapter == null) {
// Device does not support BlueHelper
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This device does not have a bluetooth adapter!")
.setTitle("Error in connecting");
AlertDialog dialog = builder.create();
dialog.show();
}