您好,登錄后才能下訂單哦!
新建一個工程“blt”。
-----------------------MainActivity.java
package com.example.blt; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.UUID; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB"; // 適配器 private BluetoothAdapter blueadapter = null; // 設備地址列表 private List<String> deviceList = new ArrayList<String>(); private ArrayAdapter<String> adapter; private ListView deviceListview; private boolean isFind = false; // 存儲設備信息 private List<UserDriver> userDrivers = new ArrayList<UserDriver>(); // 連接設備 public static BluetoothSocket btSocket; int useIndex; ProgressDialog pDialog; private long exitTime = 0; @Override protected void onCreate(Bundle savedInstanceState) { pDialog = new ProgressDialog(MainActivity.this); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setView(); setBluetooth(); } // 設置列表信息 private void setView() { adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, deviceList); deviceListview = (ListView) findViewById(R.id.listView1); deviceListview.setAdapter(adapter); deviceListview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { useIndex = position; // TODO Auto-generated method stub new AlertDialog.Builder(MainActivity.this) .setTitle("提示框") .setMessage( "確認連接?" + userDrivers.get(useIndex).DeviceAdapter .getName()) .setPositiveButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // 判斷是否需要配對 switch (userDrivers.get(useIndex).DeviceAdapter .getBondState()) { case BluetoothDevice.BOND_BONDING: Log.d("BlueToothTestActivity", "正在配對......"); break; case BluetoothDevice.BOND_BONDED: Log.d("BlueToothTestActivity", "完成配對"); LinkDevice(); break; case BluetoothDevice.BOND_NONE: Log.d("BlueToothTestActivity", "未配對"); ApadeDevice(); default: break; } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).show(); } }); } /** * 第一步,設置藍牙開啟 */ private void setBluetooth() { blueadapter = BluetoothAdapter.getDefaultAdapter(); if (blueadapter != null) { // Device support Bluetooth // 確認開啟藍牙 if (!blueadapter.isEnabled()) { // 請求用戶開啟 Intent intent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, RESULT_FIRST_USER); // 使藍牙設備可見,方便配對 // Intent in = new Intent( // BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); // in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, // 200); // startActivity(in); // 直接開啟,不經過提示 blueadapter.enable(); } findAvalibleDevice(); } else { // Device does not support Bluetooth AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("硬件錯誤"); dialog.setMessage("未找到藍牙設備"); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.show(); } } /** * 第二步,搜索設備 */ private void findAvalibleDevice() { // 獲取可配對藍牙設備 Set<BluetoothDevice> device = blueadapter.getBondedDevices(); userDrivers.clear(); deviceList.clear(); adapter.notifyDataSetChanged(); if (device.size() > 0) { // 存在已經配對過的藍牙設備 for (Iterator<BluetoothDevice> it = device.iterator(); it.hasNext();) { BluetoothDevice btd = it.next(); userDrivers.add(new UserDriver(btd, true)); deviceList.add(btd.getName() + "(已配對)\n" + btd.getAddress()); adapter.notifyDataSetChanged(); } // 搜索沒有配對的設備。 } // else { // 不存在已經配對過的藍牙設備 // deviceList.add("No can be matched to use bluetooth"); // adapter.notifyDataSetChanged(); // } } /** * 第三步。查找其他 */ public void FindOthers() { // 如果正在搜索,就先取消搜索 if (blueadapter.isDiscovering()) { blueadapter.cancelDiscovery(); } if (isFind) return; isFind = true; findAvalibleDevice(); // 注冊用以接收到已搜索到的藍牙設備的receiver IntentFilter mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, mFilter); // 注冊搜索完時的receiver mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mReceiver, mFilter); setProgressBarIndeterminateVisibility(true); setTitle("正在掃描...."); // 開始搜索藍牙設備,搜索到的藍牙設備通過廣播返回 blueadapter.startDiscovery(); } /** * 廣播處理 **/ private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); // 獲得已經搜索到的藍牙設備 if (action.equals(BluetoothDevice.ACTION_FOUND)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // 搜索到的不是已經綁定的藍牙設備 if (device.getBondState() != BluetoothDevice.BOND_BONDED) { // 顯示在TextView上 userDrivers.add(new UserDriver(device, false)); deviceList.add(device.getName() + "(未配對)\n" + device.getAddress()); adapter.notifyDataSetChanged(); } // 搜索完成 } else if (action .equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { setProgressBarIndeterminateVisibility(false); setTitle("掃描結束"); unReceive(); isFind = false; } } }; /** * 關閉注冊事件 */ private void unReceive() { try { // 解除注冊 unregisterReceiver(mReceiver); } catch (Exception e) { // TODO: handle exception } } /** * 配對并且連接設備 */ private void ApadeDevice() { try { Method createBondMethod = BluetoothDevice.class .getMethod("createBond"); Log.d("BlueToothTestActivity", "開始配對1"); try { createBondMethod .invoke(userDrivers.get(useIndex).DeviceAdapter); Log.d("BlueToothTestActivity", "開始配對2"); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub BluetoothDevice bDevice = userDrivers.get(useIndex).DeviceAdapter; while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 判斷配對狀態,更新列表信息。 if (bDevice.getBondState() == BluetoothDevice.BOND_BONDED) { Log.d("BlueToothTestActivity", "成功配對"); userDrivers.get(useIndex).IsConnected = true; deviceList.set(useIndex, bDevice.getName() + "(已配對)\n" + bDevice.getAddress()); Log.d("BlueToothTestActivity", "成功配對2"); handler2.sendMessage(new Message()); break; } } } }).start(); } 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(); } } catch (NoSuchMethodException e) { // TODO Auto-generated catch // block e.printStackTrace(); } } @SuppressLint("HandlerLeak") Handler handler2 = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); adapter.notifyDataSetChanged(); LinkDevice(); } }; private void LinkDevice() { pDialog.setMessage("連接中...."); pDialog.show(); new Thread(new Runnable() { @Override public void run() { // // TODO Auto-generated // // connect(userDrivers.get(useIndex).DeviceAdapter); Message msg = new Message(); Bundle data = new Bundle(); UUID uuid = UUID.fromString(SPP_UUID); try { if (btSocket != null && btSocket.isConnected()) btSocket.close(); btSocket = userDrivers.get(useIndex).DeviceAdapter .createRfcommSocketToServiceRecord(uuid); Log.d("BlueToothTestActivity", "開始連接..."); btSocket.connect(); if (btSocket.isConnected()) { data.putString("Request", "連接成功"); } else { data.putString("Request", "連接失敗"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); data.putString("Request", "發生錯誤"); } msg.setData(data); handler.sendMessage(msg); } }).start(); } @SuppressLint("HandlerLeak") Handler handler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); Bundle data = msg.getData(); String valString = data.getString("Request"); if (valString.equals("連接成功")) { Intent intent = new Intent(MainActivity.this, Connected.class); startActivity(intent); } else { Toast.makeText(getApplicationContext(), valString, Toast.LENGTH_SHORT).show(); // } if (pDialog.isShowing()) pDialog.cancel(); } }; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { if ((System.currentTimeMillis() - exitTime) > 2000) { Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show(); exitTime = System.currentTimeMillis(); } else { if (btSocket != null && btSocket.isConnected()) try { btSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finish(); System.exit(0); } return true; } return super.onKeyDown(keyCode, event); } @Override protected void onDestroy() { // TODO Auto-generated method stub unReceive(); super.onDestroy(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.item1) { // 搜索設備 FindOthers(); return true; } return super.onOptionsItemSelected(item); } }
界面XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.blt.MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </ListView> </RelativeLayout>
-------------------------------------------藍牙的發送和接收!Connected窗體
package com.example.blt; import java.io.InputStream; import java.io.OutputStream; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.method.ScrollingMovementMethod; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; public class Connected extends Activity { String sendData = ""; boolean _runThread = true; private TextView mResults; Thread thread = new Thread(new Runnable() { @Override public void run() { Log.d("BlueToothTestActivity", "啟動線程"); String bufferString = ""; // TODO Auto-generated method stub while (_runThread) { if (sendData != "") { Log.d("BlueToothTestActivity", "準備數據" + sendData); try { OutputStream outputStream = MainActivity.btSocket .getOutputStream(); Log.d("BlueToothTestActivity", "獲得輸出流"); outputStream.write(sendData.getBytes()); Log.d("BlueToothTestActivity", "寫入輸出流"); bufferString = "send:" + sendData; Log.d("BlueToothTestActivity", "已經發送"); } catch (Exception e) { // TODO: handle exception Log.d("BlueToothTestActivity", "發送錯誤"); } sendData = ""; } try { InputStream inputStream = MainActivity.btSocket .getInputStream(); int count = inputStream.available(); if (count > 0) { byte[] b = new byte[count]; inputStream.read(b); bufferString = "rece:" + new String(b); } } catch (Exception e) { // TODO: handle exception } if (bufferString == null || bufferString == "") continue; Message msg = new Message(); Bundle data = new Bundle(); data.putString("Request", bufferString); msg.setData(data); handler.sendMessage(msg); bufferString = ""; } } }); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_connected); mResults = (TextView) findViewById(R.id.axis_x); mResults.setMovementMethod(ScrollingMovementMethod.getInstance()); mResults.setText("日志信息"); thread.start(); ((Button) findViewById(R.id.BtnSend)) .setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d("BlueToothTestActivity", "點擊按鈕"); sendData = ((EditText) findViewById(R.id.editText1)) .getText().toString(); } }); ((EditText) findViewById(R.id.editText1)) .setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { // TODO Auto-generated method stub Log.d("BlueToothTestActivity", "鍵盤發送"); sendData = ((EditText) findViewById(R.id.editText1)) .getText().toString(); return false; } }); } // 通信模塊 @SuppressLint("HandlerLeak") Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); Bundle data = msg.getData(); String valString = data.getString("Request"); mResults.setText(mResults.getText().toString() + "\r\n" + valString); /* * Log.d("BlueToothTestActivity", mResults.getScrollX() + "," + * mResults.getScrollY()); */ } }; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { // do something... _runThread = false; Log.d("BlueToothTestActivity", "返回了"); } return super.onKeyDown(keyCode, event); } /* * private byte[] getHexBytes(String message) { int len = message.length() / * 2; char[] chars = message.toCharArray(); String[] hexStr = new * String[len]; byte[] bytes = new byte[len]; for (int i = 0, j = 0; j < * len; i += 2, j++) { hexStr[j] = "" + chars[i] + chars[i + 1]; bytes[j] = * (byte) Integer.parseInt(hexStr[j], 16); } return bytes; } */ @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.connected, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
--------------------------窗體部分
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.blt.Connected" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/BtnSend" android:digits="0123456789.abcdefghigklmnopqrstuvwxyz" android:ems="10" android:imeOptions="actionSend" android:inputType="textPersonName" > <requestFocus /> </EditText> <Button android:id="@+id/BtnSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/editText1" android:layout_alignBottom="@+id/editText1" android:layout_alignParentRight="true" android:text="Send" /> <TextView android:id="@+id/axis_x" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/BtnSend" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignRight="@+id/BtnSend" android:scrollbars="vertical" android:text="TextView" /> </RelativeLayout>
UserDriver類******************
package com.example.blt; import android.bluetooth.BluetoothDevice; public class UserDriver { //設備信息 public BluetoothDevice DeviceAdapter = null; //是否已經配對了 public boolean IsConnected = false; public UserDriver(BluetoothDevice valueDevice, boolean isConnected) { this.DeviceAdapter=valueDevice; this.IsConnected=isConnected; } }
*************************************************
載入main窗體的時候系統自動偵測是否有藍牙模塊可以使用,當存在的時候,調出已經配對過的設備列表。然后點擊菜單里面的掃描。如果掃描到新設備,則加入。
點擊設備的時候,如果沒有配對的,重新配對,配對過了就跳轉到連接界面去。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。