他在 Android 中实现了从 Arduino 接收数据的蓝牙连接,没有问题。卖家 Amarino 穿着 Listener,我做得非常好,我工作得很好。当我意识到我已经安装了 apk Amarino 是必要的并充当我的应用程序服务时,问题就开始了。这显然是不得不安装外部应用程序的痛苦,所以我开始努力寻找解决方案。
我做的第一件事是寻找一个名为 Amarino Embed 的库,它应该不需要运行应用程序 apk。该网站不是最新的,并且在数千个示例和事情不起作用之后退出。无论如何,我像 Amarino 一样上去了蓝牙通知,在这之间让我很有趣并且没有工作,我去了 B 计划。
B计划是根据Android文档来实现它,我已经阅读并广泛测试了它,实现告诉我什么线程来分离主线程的执行并使用处理程序。起初我认为如果它有效并且一切都已排序并且我会实现我在网上看到的这个(我放了松散的部分):
private BluetoothAdapter btAdapter = null; private BluetoothSocket btSocket = null; private StringBuilder sb = new StringBuilder(); private ConnectedThread mConnectedThread; private static String address = "00:12:10:17:02:24"; private Handler h; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txtArduino = (TextView) findViewById(R.id.textView1); // for display the received data from the Arduino h = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case RECIEVE_MESSAGE: // if receive massage byte[] readBuf = (byte[]) msg.obj; String strIncom = new String(readBuf, 0, msg.arg1); // create string from bytes array sb.append(strIncom); // append string int endOfLineIndex = sb.indexOf("\r\n"); // determine the end-of-line // Log.d("CADENA: ", "SB: " + sb); if (endOfLineIndex > 0) { // if end-of-line, String sbprint = sb.substring(0, endOfLineIndex); // extract string sb.delete(0, sb.length()); //Log.d("CADENA MAXIMA: ", "CM: " + sbprint);// and clear String[] cadenaSepara = sbprint.split(","); if(cadenaSepara != null) { Log.d("TAMAÑO: ", "TAM: " + cadenaSepara.length); } txtArduino.setText("Data from Arduino: " + sbprint); // update TextView } //Log.d(TAG, "...String:"+ sb.toString() + "Byte:" + msg.arg1 + "..."); break; } }; }; btAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter checkBTState(); } @Override public void onResume() { BluetoothDevice device = btAdapter.getRemoteDevice(address); btSocket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); btAdapter.cancelDiscovery(); btSocket.connect(); mConnectedThread = new ConnectedThread(btSocket); mConnectedThread.start(); } private class ConnectedThread extends Thread { private final InputStream mmInStream; private final OutputStream mmOutStream; public ConnectedThread(BluetoothSocket 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(); tmpOut = socket.getOutputStream(); } catch (IOException e) { } mmInStream = tmpIn; mmOutStream = tmpOut; //Log.d("THREAAAAAAAAD: ", "THREAAAAAAAAD: "); } public void run() { byte[] buffer = new byte[256]; // 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); // Log.d("BYTEEEES: ", "BIITES: " + bytes);// Get number of bytes and message in "buffer" h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget(); // Send to message queue Handler } catch (IOException e) { break; } } } <code>
这是一种稍微简化并省略了内部接口 MindListenerTWO 的 BROADCAST:
<pre> public class MindInterfaceTWO extends BroadcastReceiver { private static BluetoothDevice dispositivo; private static List<MindListenerTWO> listeners = new ArrayList<MindListenerTWO>(); public void addListener(MindListenerTWO mindListener) { listeners.add(mindListener); if (listeners != null && !listeners.isEmpty()) //context.registerReceiver(this, new IntentFilter(BluetoothDevice.ACTION_FOUND)); context.registerReceiver(this, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); } @Override public void onReceive(Context context, Intent intent) { //String accion = intent.getAction(); //Log.d("DEBUG:", "LLEGAAAAAA AQUIIIII"); //dispositivo = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //dispositivo.getAddress(); Log.d("LLEGA: ", "AQUIIIIIIIIIII"); // Here's what it did with AMARINO String data = null; // Esta cadena (que no se usa) simplemente muestra la direccion del // Bluetooth desde que se ha // enviado la informacion // final String address = // intent.getStringExtra(AmarinoIntent.EXTRA_DEVICE_ADDRESS); // Es el tipo de dato que se ha añadido al intento final int dataType = intent.getIntExtra( AmarinoIntent.EXTRA_DATA_TYPE, -1); Log.d("DATAAAAAAS ANTES DE ENTRAR: ", "DATOS: "); // Esto comprueba que es String en Android y char[] en Arduino para // que sean correctos los datos if (dataType == AmarinoIntent.STRING_EXTRA) { data = intent.getStringExtra(AmarinoIntent.EXTRA_DATA); Log.d("DATAAAAAA: ", "DATOS: " + data); // Formato de los datos recibidos: // signal strength, attention, meditation, delta, theta, low // alpha, high alpha, low beta, high beta, low gamma, high gamma" String[] datos = data.split(","); if (data != null) { signal = Integer.valueOf(datos[0]); launchNewSignal(signal); } } <code>
最终,我想要的是通过广播获得信号并捕获数据。我知道这里的一切都是一个小滚动评论,但是嘿,我有点疯了。问候,谢谢!
(对不起,我不能在邮政编码中好起来)