Так что я совершенно новый для java, поэтому будьте нежны. Я пытаюсь подключить два телефона по bluetooth. Я создал сокет, но как его использовать? Я имею в виду, что в руководстве Bluetooth говорится, что я должен называть public void write (byte [] bytes) для отправки данных, но как? я создал кнопку, назначил метод «onClick», но что тогда? как я должен вызвать метод, который находится в потоке пользовательского интерфейса ConnectedThread? Вот пример из учебника частный класс ConnectedThread extends Thread { закрытый финальный BluetoothSocket mmSocket; закрытый конечный InputStream mmInStream; закрытый конечный OutputStream mmOutStream;как назвать метод потоков
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
Message msg = mainHandler.obtainMessage();
Bundle bundle = new Bundle();
String btnTxt = "Socket aquizaired";
bundle.putString("myKey", btnTxt);
msg.setData(bundle);
mainHandler.sendMessage(msg);
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
// mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
и в потоке пользовательского интерфейса я имею что-то вроде
private void sendSms(){ // method assigned to button "onClick"
// i want to send some text here, like "hello" or something...
//???????????????????????????
}
Вам необходимо создать поток и передать параметр в SendSms – Charlie