由此感慨android sdk 真是武装到了牙齿了。
一开始我们要用Android控制一片msp430然后再由msp430通过无线模块控制另外一片430。
android联430的方法有几个 uart,wifi,bluetooth.其中uart用到ndk,ndk 又老是从google下不下来,wifi模块貌似很贵,而手头刚好有个bluetooth模块,于是就开始折腾。
首先是Android方面的蓝牙。
系统开启蓝牙 搜索设备 搜到了一个linvor的设备 然后就连上了,顺利的令人难以置信。
然后就开始到处搜索Android蓝牙的例程。
http://www.cnblogs.com/freeliver54/archive/2011/12/13/2285980.html
http://blog.csdn.net/menghnhhuan/article/details/7057484
很简单就几行代码,而且我只要客户端的,所以一下就弄出来了。
其中就有一行比较令人困惑
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
而且连接的时候报 Service discovery failed.
一开始还以为单片机那块要搭一个类似于socket的服务端。
然后就去下了csr bc417的数据手册,结果就傻眼了,明明是国内出的,内容却是英文的,而且全是电气性质,掩面。。
然后就搜Service discovery failed.
就解决了问题,不同的设备有不同的uuid。
csr bc477是00001101-0000-1000-8000-00805F9B34FB
应该是通用的串口设备
http://zhidao.baidu.com/question/524766279.html
然后就连接成功了
而且连接成功后csr bc417会停止闪烁。
PS.蓝牙要两个权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
package axlecho.bluetooth2msp430; import java.io.IOException; import java.util.UUID; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import com.example.bluetooth2msp430.R; public class MainActivity extends Activity implements OnClickListener { private Button btnFind = null; BluetoothAdapter adapter = null; ConnectThread connectThread = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnFind = (Button) findViewById(R.id.btn_find); btnFind.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override protected void onDestroy() { unregisterReceiver(mReceiver); super.onDestroy(); } @Override public void onClick(View v) { if (R.id.btn_find == v.getId()) { adapter = BluetoothAdapter.getDefaultAdapter(); if (null == adapter) { Log.e("axlecho", "bluetooth not find."); return; } if (!adapter.isEnabled()) { Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(intent); } IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); adapter.startDiscovery(); } } private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.i("axlecho", device.getName() + ":" + device.getAddress()); if (device.getName().equals("linvor")) { Log.i("axlecho", "connecting to linvor"); connectThread = new ConnectThread(device); connectThread.start(); } } } }; private class ConnectThread extends Thread { private final BluetoothSocket mmSocket; private final BluetoothDevice mmDevice; public ConnectThread(BluetoothDevice device) { // Use a temporary object that is later assigned to mmSocket, // because mmSocket is final BluetoothSocket tmp = null; mmDevice = device; // Get a BluetoothSocket to connect with the given BluetoothDevice try { // MY_UUID is the app's UUID string, also used by the servercode // This uuid is specail for the remote device; tmp = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); // tmp = device.createRfcommSocketToServiceRecord(UUID.fromString("00001102-0000-1000-8000-00805F9B34FB")); } catch (IOException e) { Log.e("axlecho", "createRfcommSocketToServiceRecord failed:" + e.getMessage()); } mmSocket = tmp; } public void run() { // Cancel discovery because it will slow down the connection adapter.cancelDiscovery(); try { // Connect the device through the socket. This will block // until it succeeds or throws an exception mmSocket.connect(); Log.i("axlecho", "connect succeed."); } catch (IOException connectException) { // Unable to connect; close the socket and get out Log.e("axlecho", "connect to remote bluetooth failed." + connectException.getMessage()); try { mmSocket.close(); } catch (IOException closeException) { } return; } // Do work to manage the connection (in a separate thread) // manageConnectedSocket(mmSocket); } /** Will cancel an in-progress connection, and close the socket */ public void cancel() { try { mmSocket.close(); } catch (IOException e) { } } } }