|
楼主 |
发表于 2011-3-6 01:03:49
|
显示全部楼层
相当简化的Android手机上读取NXT电池电量的小程序
刚才做出来的小程序。把蓝牙连接的部分做了相当大的简化,基本上只保留了读写的部分。Java 主程序:
- package com.TstBluetooth;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Iterator;
- import java.util.Set;
- 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.View;
- import android.widget.TextView;
- public class TstBluetooth extends Activity {
- private BroadcastReceiver btMonitor = null;
- private BluetoothAdapter btInterface;
- private Set<BluetoothDevice> pairedDevices;
-
- final String tag = "TstBluetooth";
- private BluetoothSocket socket;
- private InputStream is = null;
- private OutputStream os = null;
- private boolean bConnected = false;
- TextView tv1, tv2, tv3;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- tv1 = (TextView)findViewById(R.id.textView1);
- tv2 = (TextView)findViewById(R.id.textView2);
- tv3 = (TextView)findViewById(R.id.textView3);
- tv1.setText("Test Bluetooth...");
-
- // Setup btMonitor, to handle ACL_CONNECTED and ACL_DISCONNECTED event
- setupBTMonitor();
- }
-
- @Override
- protected void onResume()
- {
- super.onResume();
- Log.i(tag, "OnResume");
-
- int iCount;
-
- registerReceiver(btMonitor, new IntentFilter("android.bluetooth.device.action.ACL_CONNECTED"));
- registerReceiver(btMonitor, new IntentFilter("android.bluetooth.device.action.ACL_DISCONNECTED"));
- tv1.setText("onResume!");
- findRobot(null);
- }
- @Override
- protected void onPause() {
- super.onPause();
- Log.i(tag, "OnPause");
- disconnectFromRobot(null);
- unregisterReceiver(btMonitor);
- }
- private void setupBTMonitor()
- {
- btMonitor = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent){
- if (intent.getAction().equals("android.bluetooth.device.action.ACL_CONNECTED")) {
- handleConnected();
- }
- if (intent.getAction().equals("android.bluetooth.device.action.ACL_DISCONNECTED")) {
- handleDisConnected();
- }
- }
- };
- }
-
- private void handleConnected() {
- try
- {
- is = socket.getInputStream();
- os = socket.getOutputStream();
- bConnected = true;
- readNXTVoltage();
- }
- catch (Exception e)
- {
- is = null;
- os = null;
- disconnectFromRobot(null);
- }
- }
-
- private void handleDisConnected()
- {
- bConnected = false;
- }
-
- public void findRobot(View v)
- {
- try
- {
- btInterface = BluetoothAdapter.getDefaultAdapter();
- Log.i(tag, "Local BT Interface name is [" + btInterface.getName() + "]");
- tv1.setText("Local BT Interface name is [" + btInterface.getName() + "]");
- pairedDevices = btInterface.getBondedDevices();
- Log.i(tag, "Found [" + pairedDevices.size() + "] devices.");
- tv1.setText("Found [" + pairedDevices.size() + "] devices.");
- Iterator<BluetoothDevice> it = pairedDevices.iterator();
- while (it.hasNext())
- {
- BluetoothDevice bd = it.next();
- Log.i(tag, "Name of peer is [" + bd.getName() + "]");
- if (bd.getName().equalsIgnoreCase("QIQI"))
- {
- Log.i(tag, "Found Robot!");
- Log.i(tag, bd.getAddress());
- Log.i(tag, bd.getBluetoothClass().toString());
- tv1.setText("Found Robot!");
- tv2.setText(bd.getAddress());
- tv3.setText(bd.getBluetoothClass().toString());
- connectToRobot(bd);
- return;
- }
- }
- }
- catch (Exception e)
- {
- Log.e(tag, "Failed in findRotob()" + e.getMessage());
- }
- }
-
- private void connectToRobot(BluetoothDevice bd)
- {
- try
- {
- socket = bd.createRfcommSocketToServiceRecord(java.util.UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
- socket.connect();
- }
- catch (Exception e)
- {
- Log.e(tag, "Error interfacting with remote device [" + e.getMessage());
- }
- }
-
- public void disconnectFromRobot(View v)
- {
- try
- {
- Log.i(tag, "Attemting to break BT connetion");
- socket.close();
- }
- catch (Exception e)
- {
- Log.e(tag, "Error in DoDisconnect [" + e.getMessage() + "]");
- }
- }
-
- private void readNXTVoltage()
- {
- try
- {
- tv1.setText("readNXTVoltage...");
- byte[] buffer = new byte[14];
- buffer[0] = 0x2;
- buffer[1] = 0x0;
- buffer[2] = 0x0;
- buffer[3] = 0xB;
- os.write(buffer);
- os.flush();
- tv1.setText("write to NXT...");
- Thread.sleep(1000);
- byte[] sizebuffer = new byte[2];
- int bytesRead = is.read(sizebuffer, 0, 2);
- int bytesReady = sizebuffer[0];
- tv1.setText("bytesReady = " + bytesReady + "[ " + sizebuffer[0] + " " + sizebuffer[1] + " ]");
- byte [] retBuf = new byte[bytesReady];
- bytesRead = is.read(retBuf);
- int LowByte = (retBuf[3]>=0? retBuf[3] : 256 + retBuf[3]);
- int HighByte = (retBuf[4]>=0? retBuf[4] : 256 + retBuf[4]);
- tv2.setText("[ " + LowByte + " " + HighByte + "]");
- int sVolt = HighByte * 256 + LowByte;
- tv3.setText("Volt = " + sVolt + " mV");
- }
- catch (Exception e)
- {
- Log.e(tag, "Error write buffer!");
- }
- }
- }
复制代码
|
|