经过了一天的奋斗 终于然Android连上了csr bc417,一个国人开发的蓝牙模块,就是传说中的单片机。。  

由此感慨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.

android那点事--连上单片机蓝牙模块(csr bc417)_第1张图片

一开始还以为单片机那块要搭一个类似于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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnFind = (Button) findViewById(R.id.btn_find);btnFind.setOnClickListener(this);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;}@Overrideprotected void onDestroy() {unregisterReceiver(mReceiver);super.onDestroy();}@Overridepublic 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 finalBluetoothSocket tmp = null;mmDevice = device;// Get a BluetoothSocket to connect with the given BluetoothDevicetry {// 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 connectionadapter.cancelDiscovery();try {// Connect the device through the socket. This will block// until it succeeds or throws an exceptionmmSocket.connect();Log.i("axlecho", "connect succeed.");} catch (IOException connectException) {// Unable to connect; close the socket and get outLog.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) {}}}}



更多相关文章

  1. android实现蓝牙耳机的连接及列表的管理
  2. android手机通过串口蓝牙透传模块与AVR单片机通信实例。。。蓝牙
  3. Android 获取无线蓝牙MAC信息代码
  4. Android编译系统模块中的LOCAL_XXX变量
  5. Android实现蓝牙打印
  6. 根据文件名称修改安卓默认的蓝牙接收文件地址
  7. 详解Android核心模块及相关技术
  8. Android 中文API (65) ―― BluetoothClass[蓝牙]
  9. Android多模块构建合并aar解决方案

随机推荐

  1. android 自动化(1)
  2. 解决java.lang.RuntimeException: Unable
  3. Android的简易弹幕
  4. Android(安卓)Compatibility zoom dialog
  5. android 新增一個廣播偵聽USB設備的插拔
  6. android -- 多级目录创建
  7. android [1_ManagingProjects]
  8. Android编译过程详解(二)
  9. Android(安卓)知识总结
  10. android中View.measure方法详解