重新打开应用后,Android无法连接到NXT

时间:2015-03-07 09:09:01

标签: android bluetooth nxt reconnect

首先,不可思议的是我的英语不太好,但我希望你能理解我的问题是什么:) 所以,我是新手,也是Android新手。我有两部手机:摩托罗拉Defy(有Cyanogenmond-安卓4.4.2)和索尼Xperia J(Android 4.1.2 - 没有根)。 我创建了一个应用程序,可以通过蓝牙连接手机与乐高Mindstorm NXT。第一次,它在Xperia J上完美运行。它连接到NXT,当我关闭NXT时,手机试图重新连接。它似乎工作,但我关闭了应用程序并重新打开了一些。之后,Xperia J无法连接到NXT,但我不知道为什么?我试图删除所有存储数据(因为SharedPreferences),但也没有工作。然后我重新启动了手机,它似乎再次完美连接。 我以为我编码错了,但在另一部手机上没有问题。 有人可以帮我解决问题吗? (我是学生,所以如果有人知道如何重新连接到NXT,那么请告诉我:D)


MainActivity:

public class MainActivity extends ActionBarActivity {

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what) {
            case BTCommunicator.DATA_RECEIVED:
                byte[] readBuf = (byte[]) msg.obj;
                String readMessage = new String(readBuf, 0, msg.arg1);
                break;
            case BTCommunicator.CONNECTION_SUCCESSFULL:
                Toast.makeText(MainActivity.this, "Successful connection", Toast.LENGTH_LONG).show();
                BTCommunicator.getInstance().start();
                break;
            case BTCommunicator.CONNECTION_FAILED:
                Toast.makeText(MainActivity.this, "Connection failed", Toast.LENGTH_SHORT).show();
                try {
                    Thread.sleep(2000);
                } catch (Exception ex) {}
                BTCommunicator.getInstance().cancel();
                connectToDevice();
                break;
            case BTCommunicator.CONNECTION_CLOSED:
                Toast.makeText(MainActivity.this, "Connection closed", Toast.LENGTH_SHORT).show();
                BTCommunicator.getInstance().cancel();
                connectToDevice();
                break;
        }
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
protected void onPause() {
    BTCommunicator.getInstance().cancel();
    super.onPause();
}

public void allConnection(View v) {
    connectToDevice();
}

public void connectToDevice() {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
   String btAddress = sharedPreferences.getString(DeviceFounder.KEYBTADDRESS, "");
    String selectedUUID = sharedPreferences.getString(DeviceFounder.KEYUUID, "");


    Set<BluetoothDevice> pairedDevices = BTCommunicator.getInstance().getBluetoothAdapter().getBondedDevices();
    boolean deviceFound = false;
    for(BluetoothDevice device : pairedDevices) {
        if(device.getAddress().equals(btAddress)) {
            new BTConnectAsyncTask(
                     this, handler, device, selectedUUID).execute();
            deviceFound = true;
        }
    }

    if(!deviceFound) {
        Toast.makeText(this, "Device is not paired: " + btAddress, Toast.LENGTH_LONG).show();
    }
}

public void showDeviceFounder(View v) {
    startActivity(new Intent(this, DeviceFounder.class));
}

}


DeviceFounder类

public class DeviceFounder extends Activity {

private DeviceListAdapter listAdapter;
public static final String KEYBTADDRESS = "BTADDRESS"; //BT MAC-cím
public static final String KEYUUID = "UUID";
public static final String UUIDVALUE = "00001101-0000-1000-8000-00805F9B34FB";
public static String KEYBTNAME = "BTNAME";

private final BroadcastReceiver foundReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        //Ha talált új eszközt, beteszi
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            //Le akarjuk tölteni az adatokat
            BluetoothDevice device =
                    intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            listAdapter.addDevice(device);
            listAdapter.notifyDataSetChanged();
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_device_founder);

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(foundReceiver, filter);

    BTCommunicator.getInstance().getBluetoothAdapter().startDiscovery();

    ListView listView = (ListView)findViewById(R.id.listView1);
    listAdapter = new DeviceListAdapter();
    listView.setAdapter(listAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            sendSelectedUUID(position);
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(foundReceiver);
    BTCommunicator.getInstance().cancel();
}

private void sendSelectedUUID(final int aSelectPosition) {
    BluetoothDevice device = (BluetoothDevice) listAdapter.getItem(aSelectPosition);

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(KEYBTADDRESS, device.getAddress());
    editor.putString(KEYUUID, UUIDVALUE);
    editor.putString(KEYBTNAME, device.getName());
    editor.commit();
    finish();
}

}

BTConnectAsyncTask只需调用:

 try {
        UUID serviceUuid = UUID.fromString(selectedUUID);
        BTCommunicator.getInstance().connect(device, serviceUuid, handlerStatus);
        return "OK";
    } catch (Exception e) {
        return ("Error: "+e.getMessage());
    }

最后是BTCommunicator课程:

'public class BTCommunicator extends Thread {
public static final int DATA_RECEIVED = 0;
public static final int CONNECTION_SUCCESSFULL = 1;
public static final int CONNECTION_FAILED = 2;
public static final int CONNECTION_CLOSED = 3;

//Ez reprezentálja a fizikai BlueTooth adaptert. Ezt használva
//tudod felfedni a többi eszközt MAC-kel és BlueToothServerSocket
//segítségével figyelni a bejövő kommunikációt!
private BluetoothAdapter bluetoothAdapter = null;
//Ezen keresztül engedi a rendszer, hogy egy app kommunikáljon egy
//másik appal. Input- és OutputStreamen keresztül.
private InputStream inStream = null;
private OutputStream outStream = null;
private Handler msgHandler = null;
private static boolean enabled = false;
public static  BluetoothSocket clientSocket = null;

//statikus példány kérése
private static BTCommunicator instance = null;

public static BTCommunicator getInstance() {
    if(instance == null) {
        instance = new BTCommunicator();
    }
    return instance;
}

protected BTCommunicator() {
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}

public BluetoothAdapter getBluetoothAdapter() {
    return bluetoothAdapter;
}

public void connect(BluetoothDevice device, UUID uuid, Handler
        handler)
{
    bluetoothAdapter.cancelDiscovery();
    msgHandler = handler;
    enabled = true;

    try {
        clientSocket = device.createRfcommSocketToServiceRecord(uuid);
        clientSocket.connect();
        inStream = clientSocket.getInputStream();
        outStream = clientSocket.getOutputStream();
        handler.obtainMessage(CONNECTION_SUCCESSFULL,
                "").sendToTarget();
    } catch (Exception e) {
        Log.e("BTCommunicator", e.getMessage());
        try {
            handler.obtainMessage(CONNECTION_FAILED, e.getMessage
                    ()).sendToTarget();
            clientSocket.close();
           // BTCommunicator.getInstance().cancel();
            //BTCommunicator.getInstance().connect(device, uuid, handler);
        } catch (IOException closeException) { }
    }
}

public void run() {
    byte[] buffer = new byte[1024];
    int bytes;

    while (enabled) {
        try {
            bytes = inStream.read(buffer);
            msgHandler.obtainMessage(DATA_RECEIVED, bytes, -1,
                    buffer).sendToTarget();
        } catch (IOException e) {
            msgHandler.obtainMessage(CONNECTION_CLOSED,
                    "").sendToTarget();

            try {
                clientSocket.close();
            } catch (IOException e2) { }
            break;
        }
    }
}

public void write(byte[] bytes) {
    try {
        if(outStream != null) {
            outStream.write(bytes);
        }
    } catch (IOException e) {
        Log.e("BTCOMM", e.getMessage());
    }
}

public void cancel() {
    enabled = false;

    if (bluetoothAdapter != null)
        bluetoothAdapter.cancelDiscovery();

    if (msgHandler != null)
        /*msgHandler.obtainMessage(CONNECTION_CLOSED,
                "").sendToTarget();*/

    if (outStream != null) {
        try {
            outStream.close();
            outStream = null;
        } catch (Exception e) {
        }
    }

    if (inStream != null) {
        try {
            inStream.close();
            inStream = null;
        } catch (Exception e) {
        }
    }

    if (clientSocket != null) {
        try {
            clientSocket.close();
            clientSocket = null;
        } catch (IOException e) {
        }
    }

    instance = null;
}

} `

1 个答案:

答案 0 :(得分:0)

好的,问题解决了:)我应该在BTCommunicator catch块中调用整个cancel函数,因为如果关闭程序,Android 4.1.2将不会关闭流。我不知道为什么,也许是操作系统中的错误? XD因为它只是在Android 4.4.2版之前引起的。