Привет! Я использую сервис для подключения к Bluetooth, который ожидает входящего запроса.Но мой разъем отключается, как только он подключен.На моем другом клиентском устройстве я четко вижу, что оно говорит «подключено» на долю секунды, прежде чем оно отключается.Вот код, который я написал.
package com.example.nisarga.testapplication;
import android.app.IntentService;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class BlueToothService extends IntentService
{
private final String TAG="TestTag";
public BlueToothService()
{
super("BlueToothService");
}
@Override
protected void onHandleIntent( Intent intent)
{
}
@Override
public void onCreate()
{
super.onCreate();
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),"Your device does not support bluetooth",Toast.LENGTH_SHORT).show();
Log.d(TAG,"Device does not support bluetooth");
stopSelf();
}
else
{
if (!mBluetoothAdapter.isEnabled())
{
Toast.makeText(getApplicationContext(), "Please turn on the bluetooth", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Bluetooth is not turned on");
stopSelf();
}
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
}
}
AcceptThread acceptThread=new AcceptThread();
acceptThread.start();
}
private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
// Use a temporary object that is later assigned to mmServerSocket
// because mmServerSocket is final.
BluetoothAdapter mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket tmp = null;
try {
// MY_UUID is the app's UUID string, also used by the client code.
tmp = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("Samaya", UUID.fromString("9fb4c0be-2229-43e4-be74-e53860a26420"));
Log.d(TAG, "Accepting the requests");
} catch (IOException e) {
Log.e(TAG, "Socket's listen() method failed", e);
}
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned.
while (true) {
try {
socket = mmServerSocket.accept();
Log.d(TAG, socket+"");
} catch (IOException e) {
Log.e(TAG, "Socket's accept() method failed", e);
break;
}
if (socket != null) {
// A connection was accepted. Perform work associated with
// the connection in a separate thread.
Log.d(TAG, "Closing the server socket");
ConnectedThread connectedThread=new ConnectedThread(socket);
connectedThread.start();
try
{
mmServerSocket.close();
} catch (IOException e)
{
e.printStackTrace();
}
break;
}
}
}
// Closes the connect socket and causes the thread to finish.
public void cancel() {
try {
mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close the connect socket", e);
}
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private byte[] mmBuffer; // mmBuffer store for the stream
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "Connected");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams; using temp objects because
// member streams are final.
try {
tmpIn = socket.getInputStream();
} catch (IOException e) {
Log.e(TAG, "Error occurred when creating input stream", e);
}
try {
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "Error occurred when creating output stream", e);
}
mmInStream = tmpIn;
}
public void run() {
Log.d(TAG, "Waiting for inout");
mmBuffer = new byte[1024];
int numBytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs.
while (true) {
try {
// Read from the InputStream.
numBytes = mmInStream.read(mmBuffer);
Log.d(TAG,mmBuffer.toString());
} catch (IOException e) {
Log.d(TAG, "Input stream was disconnected", e);
break;
}
}
}
// Call this method from the main activity to shut down the connection.
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close the connect socket", e);
}
}
}
}
Любая помощь будет принята с благодарностью.Заранее спасибо