Home > Mobile >  Bluetooth development has search and obtain needed to connect bluetooth address why don't conne
Bluetooth development has search and obtain needed to connect bluetooth address why don't conne

Time:09-17

Already finished to bluetooth search, and by clicking on the ListView can display the search to the equipment needed to click select devices will address displayed in the textview, and then passed on address start connection device, the results did not respond? A great god please help see


//connected bluetooth devices
BtConnect=the findViewById (R.i db t_connect);
BtConnect. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View View) {
String MAcaddress=tvaddress. GetText (). The toString ();
ConnectDevice (MAcaddress);
Toast. MakeText (MainActivity. This "connected bluetooth devices." , Toast.LENGTH_SHORT).show();
}
});

//bluetooth connection
Private void connectDevice (String address) {
//determine whether in the search, if in the search, to cancel the search
Try {//get the remote device
BluetoothDevice nwdevice=bluetoothAdapter. GetRemoteDevice (address);
//connection
//mSocket=(BluetoothSocket) nwdevice. GetClass () getMethod (" createRfcommSocket ", new Class [] {int. Class}), invoke (nwdevice, 1);
MSocket=nwdevice. CreateInsecureRfcommSocketToServiceRecord (MY_UUID);
} the catch (IOException e) {
e.printStackTrace();
}
}

CodePudding user response:

Even not, see the error message

CodePudding user response:

Why use reflection to connect, the normal use function call

 
Gatts=device. ConnectGatt (instance, false, mGattCallback);

CodePudding user response:

You didn't connect, just create a socket.

CodePudding user response:

reference u010767740 reply: 3/f
you didn't connect, just create a socket.

Here are mSocket. The connect (); Not posted, various measures have been tried is not even

CodePudding user response:

reference 4 floor shenjl463 response:
Quote: reference u010767740 reply: 3/f

You didn't connect, just create a socket.

Here are mSocket. The connect (); No post, various measures have been tried is not even


How did you write your server socket,

CodePudding user response:

reference 5 floor u010767740 reply:
Quote: refer to 4th floor shenjl463 response:

Quote: refer to the third floor u010767740 response:

You didn't connect, just create a socket.

Here are mSocket. The connect (); No post, various measures have been tried is not even


How did you write your server socket,

MCurDevice=bluetoothAdapter. GetRemoteDevice (address);
}
//need to try catch here, in case the exception is thrown
Try {
//determine whether the client interface is empty
If (mSocket==null) {
//to get to the client interface
MSocket=mCurDevice. CreateRfcommSocketToServiceRecord (MY_UUID);
The e (TAG, "access the address equipment connectDevice:");
//send the connection to the server
MSocket. The connect ();
The e (TAG, "connectDevice: start connecting equipment");
//get to the output stream to write data
OS=mSocket. GetOutputStream ();
The e (TAG, "connectDevice: successful connection");
}
//determine whether to get the output stream
If (OS!=null) {
//need to send information
String text="I preach in the past";
//in utf-8 format sent out
OS. Write (text. GetBytes (" utf-8 "));
}
Toast. MakeText (this, "to send information, please check", Toast. LENGTH_LONG), show ();
} the catch (IOException e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
Toast. MakeText (this, "send information failure", Toast. LENGTH_LONG), show ();
}

CodePudding user response:

refer to 6th floor shenjl463 response:
Quote: refer to the fifth floor u010767740 reply:

Quote: refer to 4th floor shenjl463 response:

Quote: refer to the third floor u010767740 response:

You didn't connect, just create a socket.

Here are mSocket. The connect (); No post, various measures have been tried is not even


How did you write your server socket,

MCurDevice=bluetoothAdapter. GetRemoteDevice (address);
}
//need to try catch here, in case the exception is thrown
Try {
//determine whether the client interface is empty
If (mSocket==null) {
//to get to the client interface
MSocket=mCurDevice. CreateRfcommSocketToServiceRecord (MY_UUID);
The e (TAG, "access the address equipment connectDevice:");
//send the connection to the server
MSocket. The connect ();
The e (TAG, "connectDevice: start connecting equipment");
//get to the output stream to write data
OS=mSocket. GetOutputStream ();
The e (TAG, "connectDevice: successful connection");
}
//determine whether to get the output stream
If (OS!=null) {
//need to send information
String text="I preach in the past";
//in utf-8 format sent out
OS. Write (text. GetBytes (" utf-8 "));
}
Toast. MakeText (this, "to send information, please check", Toast. LENGTH_LONG), show ();
} the catch (IOException e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
Toast. MakeText (this, "send information failure", Toast. LENGTH_LONG), show ();
}

At present through code didn't see any problem with your client,
You may have a look whether there is a server-side code problem. Is there any according to MY_UUID create BluetoothServerSocket.
http://androidxref.com/9.0.0_r3/xref/frameworks/base/core/java/android/bluetooth/BluetoothAdapter.java#2018

CodePudding user response:

After obtaining the socket socket in the thread. The connect ()
  • Related