我正在尝试制作一个能够读取蓝牙远程信号的应用程序,无论它发送什么数据。我只有一个便宜的 VR 遥控器和一个快门遥控器。
配对后我可以得到他们的mac地址和UUID。
与蓝牙套接字连接的部分不断崩溃。有一个更好的方法吗?
public class SettingsActivity extends AppCompatActivity {
Button b1,b2,b3,b4;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
ListView lv;
ListView uuidlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Intent i = getIntent();
String message = i.getStringExtra("Info");
((TextView)findViewById(R.id.textView)).setText(message);
b1 =(Button)findViewById(R.id.button_on);
b2=(Button)findViewById(R.id.button_off);
b3=(Button)findViewById(R.id.list_devices);
b4=(Button)findViewById(R.id.visible);
BA = BluetoothAdapter.getDefaultAdapter();
lv = (ListView)findViewById(R.id.listView);
uuidlist = (ListView)findViewById(R.id.listView);
String uuid = get_uuid_from_paired_devices();
((TextView)findViewById(R.id.uuid)).setText(uuid);
final UUID mUUID = UUID.fromString(uuid);
String mac = get_mac_from_paired_devices();
mac = mac.replaceAll("[\\[\\](){}]","");
((TextView)findViewById(R.id.mac)).setText(mac);
//BluetoothDevice remote = BA.getRemoteDevice(mac);
BluetoothDevice remote;
BluetoothServerSocket mmServerSocket;
BluetoothSocket socket = null;
try {
mmServerSocket = BA.listenUsingRfcommWithServiceRecord("remote", mUUID);
socket = mmServerSocket.accept();
} catch (Exception e) {
}
**////////// stuck here with the bluetooth socket**
}
public void on(View v){
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 1);
Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
}
}
public void off(View v){
BA.disable();
Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();
}
public void visible(View v){
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 1);
}
public void list(View v){
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
public void unpair(View v) {
Set<BluetoothDevice> bondedDevices = BA.getBondedDevices();
try {
Class<?> btDeviceInstance = Class.forName(BluetoothDevice.class.getCanonicalName());
Method removeBondMethod = btDeviceInstance.getMethod("removeBond");
boolean cleared = false;
for (BluetoothDevice bluetoothDevice : bondedDevices) {
String mac = bluetoothDevice.getAddress();
removeBondMethod.invoke(bluetoothDevice);
Toast.makeText(this, "Cleared Pairing", Toast.LENGTH_LONG ).show();
cleared = true;
break;
}
if(!cleared) {
Toast.makeText(this,"Not paired", Toast.LENGTH_LONG ).show();
}
} catch (Throwable th) {
Toast.makeText(this,"Error pairing", Toast.LENGTH_LONG ).show();
}
}
private String get_uuid_from_paired_devices(){ //Get the list UUIDs
Set<BluetoothDevice> pairedDevices = BA.getBondedDevices();
String uuid_string = "";
for (BluetoothDevice device: pairedDevices){
for (ParcelUuid uuid: device.getUuids()){
uuid_string = uuid.toString();
}
}
return uuid_string;
}