标签:
BlueTooth_SmartCarActivity.java文件
package cn.BlueTooth_SmartCar; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.UUID; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Vibrator; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.Button; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast; public class BlueTooth_SmartCarActivity extends Activity { String information; public byte[] message = new byte[1]; private Vibrator vibrator;//震动设置 private BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice btDevice; BluetoothSocket bluetoothSocket = null; OutputStream tmpOut = null; private static final UUID MY_UUID = UUID.fromString("37849990-8AAD-4E9F-88D5-8E417CBF02C3"); private String Address = "00:12:10:31:04:08"; Button buttonUP; //方向按键 Button buttonDOWN; Button buttonLeftTop; Button buttonRightTop; Button buttonLeftDown; Button buttonRightDown; TextView textViewDeviceName; TextView textViewSuccessed; TextView textViewProgress; SeekBar seekBar; int i = 70; //初始速度值 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonUP=(Button)findViewById(R.id.buttonUP); buttonDOWN=(Button)findViewById(R.id.buttonDOWN); buttonRightTop=(Button)findViewById(R.id.buttonRightTop); buttonLeftTop=(Button)findViewById(R.id.buttonLeftTop); buttonLeftDown=(Button)findViewById(R.id.buttonLeftDown); buttonRightDown=(Button)findViewById(R.id.buttonRightDown); textViewDeviceName=(TextView)findViewById(R.id.textViewDeviceName); textViewSuccessed=(TextView)findViewById(R.id.textViewSuccessed); textViewProgress=(TextView)findViewById(R.id.textViewProgress); seekBar=(SeekBar)findViewById(R.id.seekBar); buttonUP.setOnTouchListener(new ButtonOnTouchListener()); buttonDOWN.setOnTouchListener(new ButtonOnTouchListener()); buttonLeftTop.setOnTouchListener(new ButtonOnTouchListener()); buttonRightTop.setOnTouchListener(new ButtonOnTouchListener()); buttonLeftDown.setOnTouchListener(new ButtonOnTouchListener()); buttonRightDown.setOnTouchListener(new ButtonOnTouchListener()); seekBar.setOnSeekBarChangeListener(new SeekBarListener()); //检测蓝牙适配器 if(btAdapter != null){ System.out.println("蓝牙设备驱动正常"); if(!btAdapter.isEnabled()){ Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(intent); System.out.println("适配器不可用"); } else System.out.println("适配器可用"); } else System.out.println("蓝牙设备驱动异常"); textViewProgress.setText("当前速度:"+i); seekBar.setProgress(70); } /******发送信息方法*********/ public void bluesend(byte[] msg){ try{ tmpOut = bluetoothSocket.getOutputStream(); System.out.println("生成输出流"); Log.d("send",Arrays.toString(msg)); System.out.println("正在把输出流送到缓存区"); tmpOut.write(msg); System.out.println("成功输出流送到缓存区"); }catch(IOException e){ e.printStackTrace(); System.out.println("无法生成输出流或把输出流送到缓存区"); } } /******震动10ms方法******/ public void vibrator(){ vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(20); System.out.println("震动20ms"); } /*****SeekBar********/ private class SeekBarListener implements SeekBar.OnSeekBarChangeListener{ @Override//进度条改变时 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub i = seekBar.getProgress(); textViewProgress.setText("当前速度:"+i); } @Override//开始拖动滑块时 public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub i= seekBar.getProgress(); textViewProgress.setText("当前速度:"+i); } @Override//结束拖动滑块时 public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub i= seekBar.getProgress(); textViewProgress.setText("当前速度:"+i); int k; k = i/10; if(k==10) k=9; //将整型转变为字符串型 String j = String.valueOf(k); byte[] msgBuffer = j.getBytes(); bluesend(msgBuffer); } } /********按键监听器**************/ class ButtonOnTouchListener implements OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); action = event.getAction(); switch(action){ case MotionEvent.ACTION_DOWN: switch(v.getId()){ case R.id.buttonUP: message[0] = (byte)0x41;//A的ASCII码65; vibrator();//震动10ms bluesend(message); System.out.println("前进"); break; case R.id.buttonDOWN: message[0] = (byte)0x42;//B的ASCII码66; vibrator();//震动10ms bluesend(message); System.out.println("后退"); break; case R.id.buttonLeftTop: message[0] = (byte)0x43;//S的ASCII码69; vibrator();//震动10ms bluesend(message); System.out.println("左上"); break; case R.id.buttonRightTop: message[0] = (byte)0x44;//F的ASCII码70; vibrator();//震动10ms bluesend(message); System.out.println("右上"); break; case R.id.buttonLeftDown: message[0] = (byte)0x45;//G的ASCII码71; vibrator();//震动10ms bluesend(message); System.out.println("左下"); break; case R.id.buttonRightDown: message[0] = (byte)0x46;//H的ASCII码72; vibrator();//震动10ms bluesend(message); System.out.println("右下"); break; } break; case MotionEvent.ACTION_UP: message[0] = (byte)0x61;//a的ASCII码; vibrator();//震动10ms bluesend(message); System.out.println("松手停止"); break; } return false; } } /*********菜单************/ @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, 0, 0, "退出"); menu.add(0, 1, 1, "遥控"); menu.add(0, 2, 2, "循迹"); menu.add(0, 3, 3, "红外"); menu.add(0, 4, 4, "金探"); menu.add(0, 5, 5, "测温"); menu.add(0, 6, 6, "测湿"); menu.add(0, 7, 7, "测速"); menu.add(0, 8, 8, "避障"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub byte[] msgBufferWay = new byte[1] ; if(item.getItemId()==0){ finish(); } if(item.getItemId()==1){ msgBufferWay[0] = (byte)0x62;//b的ASCII码; bluesend(msgBufferWay); } if(item.getItemId()==2){ msgBufferWay[0] = (byte)0x63;//c的ASCII码; bluesend(msgBufferWay); } if(item.getItemId()==3){ msgBufferWay[0] = (byte)0x64;//d的ASCII码;; bluesend(msgBufferWay); } if(item.getItemId()==4){ msgBufferWay[0] = (byte)0x65;//e的ASCII码;; bluesend(msgBufferWay); } if(item.getItemId()==5){ msgBufferWay[0] = (byte)0x66;//f的ASCII码;; bluesend(msgBufferWay); } if(item.getItemId()==6){ msgBufferWay[0] = (byte)0x67;//g的ASCII码;; bluesend(msgBufferWay); } if(item.getItemId()==7){ msgBufferWay[0] = (byte)0x68;//h的ASCII码;; bluesend(msgBufferWay); } if(item.getItemId()==8){ msgBufferWay[0] = (byte)0x69;//i的ASCII码;; bluesend(msgBufferWay); } return super.onOptionsItemSelected(item); } /******onResume阶段***********/ @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); btDevice = btAdapter.getRemoteDevice(Address); System.out.println("根据地址获取到远程蓝牙设备"); if(btDevice!=null){ textViewDeviceName.setText("你所连接的设备是:"+btDevice.getName()); } try { // bluetoothSocket = btDevice.createRfcommSocketToServiceRecord(MY_UUID); /*******蓝牙连接***********/ Method m; try { m = btDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); bluetoothSocket = (BluetoothSocket) m.invoke(btDevice, Integer.valueOf(1)); } catch (SecurityException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (NoSuchMethodException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("根据UUID获取到远程蓝牙设备客户端"); bluetoothSocket.connect(); System.out.println("已连接到远程设备"); textViewSuccessed.setText("设备连接:成功"); } catch (IOException e) { // TODO Auto-generated catch block textViewSuccessed.setText("设备连接:未成功"); e.printStackTrace(); } } /********销毁阶段********/ @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); try { bluetoothSocket.close(); System.out.println("正在关闭客户端"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
标签:
原文地址:http://my.oschina.net/u/1185882/blog/295222