BTClient.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.test.BTClient;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.util.UUID;
  5. import android.app.Activity;
  6. import android.bluetooth.BluetoothAdapter;
  7. import android.bluetooth.BluetoothDevice;
  8. import android.bluetooth.BluetoothSocket;
  9. import android.content.BroadcastReceiver;
  10. import android.content.Context;
  11. import android.content.Intent;
  12. import android.content.IntentFilter;
  13. import android.os.Bundle;
  14. import android.view.View;
  15. import android.widget.EditText;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18. public class BTClient extends Activity {
  19. private TextView text0;
  20. private EditText edit0;
  21. //按钮动作,监听服务端口,打开UUID服务
  22. BluetoothDevice _device = null;
  23. BluetoothSocket _socket = null;
  24. boolean _discoveryFinished = false;
  25. private BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();
  26. /** Called when the activity is first created. */
  27. @Override
  28. public void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.main);
  31. text0 = (TextView)findViewById(R.id.Text0);
  32. edit0 = (EditText)findViewById(R.id.Edit0);
  33. //打开本地设备
  34. if (_bluetooth == null){
  35. Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
  36. // finish();
  37. return;
  38. }
  39. // 设置设备可以被搜索
  40. _bluetooth.enable();
  41. if(_bluetooth.isEnabled()==false){
  42. Toast.makeText(this, "Bluetooth can`t be discorvered", Toast.LENGTH_LONG).show();
  43. //finish();
  44. return;
  45. }
  46. text0.setText("打个本地蓝牙服务成功!");
  47. }
  48. //定义广播接收器
  49. private BroadcastReceiver _foundReceiver = new BroadcastReceiver() {
  50. public void onReceive(Context context, Intent intent) {
  51. /* 从intent中取得搜索结果数据 */
  52. //BluetoothDevice _device0 = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  53. //if (_device0.getName().equals("linvor") )
  54. _device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);;
  55. text0.setText("找寻到设备:"+_device.getName());
  56. }
  57. };
  58. private BroadcastReceiver _discoveryReceiver = new BroadcastReceiver() {
  59. @Override
  60. public void onReceive(Context context, Intent intent)
  61. {
  62. /* 卸载注册的接收器 */
  63. unregisterReceiver(_foundReceiver);
  64. unregisterReceiver(this);
  65. _discoveryFinished = true;
  66. }
  67. };
  68. //连接搜寻服务
  69. public void onSearchButtonClicked(View v){
  70. IntentFilter discoveryFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
  71. registerReceiver(_discoveryReceiver, discoveryFilter);
  72. IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  73. registerReceiver(_foundReceiver, foundFilter);
  74. new Thread(){
  75. public void run()
  76. {
  77. //开始搜索
  78. _bluetooth.startDiscovery();
  79. for (;;)
  80. {
  81. if (_discoveryFinished)
  82. {
  83. break;
  84. }
  85. try
  86. {
  87. Thread.sleep(100);
  88. }
  89. catch (InterruptedException e){}
  90. }
  91. }
  92. }.start();
  93. text0.setText("正在搜寻设备。");
  94. }
  95. //连接设备
  96. public void onConenectButtonClicked(View v){
  97. /*_device = _bluetooth.getRemoteDevice("00:10:20:26:00:04");
  98. if(_device==null){
  99. Toast.makeText(this, "Bluetooth is not found", Toast.LENGTH_LONG).show();
  100. return;
  101. }
  102. //链接设备
  103. try{
  104. _bluetooth.cancelDiscovery();
  105. _socket = _device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
  106. text0.setText("打开服务成功");
  107. }catch(IOException e){
  108. text0.setText("打开服务不成功");
  109. return;
  110. }
  111. if(_socket==null){
  112. text0.setText("打开连接失败");
  113. return;
  114. }*/
  115. try{
  116. _socket.connect();
  117. text0.setText("成功建立连接,可以发送数据!"+_device.getName());
  118. }catch(IOException e){
  119. //text0.setText(e.toString());
  120. text0.setText("连接失败");
  121. }
  122. }
  123. public void onSendButtonClicked(View v){
  124. try{
  125. OutputStream os = _socket.getOutputStream();
  126. os.write(edit0.getText().toString().getBytes());
  127. os.close();
  128. text0.setText("发送数据成功!");
  129. }catch(IOException e){
  130. }
  131. }
  132. public void onDestroy(){
  133. super.onDestroy();
  134. if(_socket!=null)
  135. try{
  136. _socket.close();
  137. }catch(IOException e){}
  138. }
  139. }