我正在尝试通过android中的编程方式连接到另一台蓝牙,但我一直在收到此错误。有人,请帮我连接蓝牙。我的代码有什么问题。我正在使用过去2周的蓝牙,但无法连接到蓝牙。 是否有工作源代码可用于github它可能对我有帮助
错误 - > BluetoothAdapter:getBluetoothService()调用withBluetoothManagerCallback
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mBluetoothDevice;
ListView paired_lv;
Button paired_btn, listen, sendData;
TextView deviceTxt;
ClientSocket socket;
ConnectedThread connectedThread;
private static final UUID MY_UUID_SECURE =
UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID_INSECURE =
UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
private static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
paired_btn = findViewById(R.id.paired_devices);
paired_lv = findViewById(R.id.paired_lv);
deviceTxt = findViewById(R.id.device_name);
listen = findViewById(R.id.listen);
sendData = findViewById(R.id.send_data);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null){
toast("Your device doesn't support bluetooth adapter");
}else if (!mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.enable();
}
paired_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pairedDevices();
}
});
paired_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
BluetoothDevice device = (BluetoothDevice) adapterView.getItemAtPosition(i);
toast("clicket"+device.getName() );
socket = new ClientSocket(device);
socket.start();
}
});
listen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// AcceptThread acceptThread = new AcceptThread();
//acceptThread.start();
if (socket != null){
socket.cancel();
}
}
});
sendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String mes = "bhanu";
Log.d("BT","Send button clicked");
connectedThread.write("bhanu");
}
});
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
registerReceiver(mReceiver,filter);
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)){
Log.d("BT","device connected");
toast("device connected");
}else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)){
toast("device disconnected");
}
}
};
public void toast(String message){
Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
}
public void pairedDevices(){
Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
ArrayList<BluetoothDevice> arrayList = new ArrayList<>();
if (devices.size()<0){
toast("no paired devices found");
}else {
for (BluetoothDevice device:devices){
toast(device.getName());
arrayList.add(device);
ArrayAdapter<BluetoothDevice> adapter = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,arrayList);
paired_lv.setAdapter(adapter);
}
}
}
private class ClientSocket extends Thread{
private final BluetoothDevice mDevice;
private final BluetoothSocket mSocket;
public ClientSocket(BluetoothDevice device,Boolean isSecure) {
mDevice = device;
BluetoothSocket tmp = null;
boolean secure = isSecure;
try {
tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
} catch (IOException e) {
e.printStackTrace();
}
mSocket = tmp;
}
@Override
public void run() {
mBluetoothAdapter.cancelDiscovery();
if (mDevice.getBondState() == BluetoothDevice.BOND_BONDED){
Log.d("BT","bonded");
}
if (BluetoothDevice.DEVICE_TYPE_LE == mDevice.getType()){
try {
Log.d("BT","Connecting BT");
Log.d("BT","socket info"+ mSocket);
mSocket.connect();
Log.d("BT","Connected BT");
} catch (IOException e) {
cancel();
e.printStackTrace();
}
}
if (mSocket != null){
connectedThread = new ConnectedThread(mSocket);
}
}
public void cancel(){
if (mSocket.isConnected()){
try {
mSocket.close();
Log.d("BT","socket was closed");
} catch (IOException e) {
e.printStackTrace();
}
}else {
Log.d("BT","device not connected to the any other device");
}
}
}
private class AcceptThread extends Thread{
private final BluetoothServerSocket mServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
try {
tmp = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("Bhanu",MY_UUID_INSECURE);
} catch (IOException e) {
e.printStackTrace();
}
mServerSocket = tmp;
}
@Override
public void run() {
BluetoothSocket socket = null;
while (true){
try {
//Toast.makeText(getApplicationContext(),"ready to accept",Toast.LENGTH_SHORT).show();
Log.d("BT","ready to accept socket");
socket = mServerSocket.accept();
Log.d("BT","socket accepted");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
}
答案 0 :(得分:0)
您需要BluetoothManager
BluetoothManager mBluetoothManager;
if (mBluetoothManager == null) {
mBluetoothManager = (BluetoothManager) getSystemService(this);
if (mBluetoothManager == null) {
Log.e(TAG, "Unable to initialize BluetoothManager.");
}
}
mBluetoothAdapter = mBluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
}