为了学习和测试蓝牙的通信机制,我是把服务器端和客户端分开为两个APP来写的。
一、首先是服务器端:
1、AndroidManifest.xml
添加权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
2、布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/open" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="打开蓝牙" /> <Button android:id="@+id/enable" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="可被检测" /> <Button android:id="@+id/receive" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="打开服务" /> <TextView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="show"/> </LinearLayout>
3、Activity
package com.study.bluetooths; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.UUID; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothServerSocket; import android.bluetooth.BluetoothSocket; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class BluetoothSActivity extends Activity { private Button open, enable, receive; private TextView show; private BluetoothAdapter adapter; private Handler handler = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); Toast.makeText(BluetoothSActivity.this, "hello", Toast.LENGTH_SHORT).show(); show.setText(msg.obj.toString()); } }; open = (Button) findViewById(R.id.open); enable = (Button) findViewById(R.id.enable); show = (TextView) findViewById(R.id.show); receive = (Button) findViewById(R.id.receive); adapter = BluetoothAdapter.getDefaultAdapter(); open.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub if (adapter != null) { if (!adapter.isEnabled()) { Intent intent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, 1); } } } }); enable.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub if ((adapter != null) && (adapter.isEnabled())) { Intent intent = new Intent( BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivity(intent); } } }); receive.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub new Thread(new Runnable() { public void run() { BluetoothServerSocket serverSocket = null; BluetoothSocket socket = null; BufferedReader in = null; try { System.out.println("1"); serverSocket = adapter .listenUsingRfcommWithServiceRecord( "XIAO", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); System.out.println("2"); socket = serverSocket.accept(); System.out.println("3"); if (socket != null) { InputStream inputStream = socket .getInputStream(); in = new BufferedReader(new InputStreamReader( inputStream)); System.out.println("4"); // 到这里有一点错误 String result; while (true) { if ((result = in.readLine()) != null) { Message msg = new Message(); msg.obj = result; handler.sendMessage(msg); } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { } } }).start(); } }); } }
1、AndroidManifest.xml
权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
2、布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/open" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="打開藍牙" /> <Button android:id="@+id/scan" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="掃描藍牙" /> <Button android:id="@+id/send" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="連接發送" /> <TextView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="show" /> </LinearLayout>
package com.study.bluetoothc; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; 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.view.View; import android.widget.Button; import android.widget.TextView; public class BluetoothCActivity extends Activity { private Button open, scan, send; private TextView show; private BluetoothAdapter adapter; private BluetoothDevice remoteDevice = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); open = (Button) findViewById(R.id.open); scan = (Button) findViewById(R.id.scan); send = (Button) findViewById(R.id.send); show = (TextView) findViewById(R.id.show); adapter = BluetoothAdapter.getDefaultAdapter(); open.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub if (adapter != null) { if (!adapter.isEnabled()) { Intent intent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, 1); } } } }); scan.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub IntentFilter filter = new IntentFilter( BluetoothDevice.ACTION_FOUND); registerReceiver(receiver, filter); if ((adapter != null) && (adapter.isEnabled())) { adapter.startDiscovery(); } } }); send.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub new Thread(new Runnable() { public void run() { if (remoteDevice == null) { return; } BluetoothSocket socket = null; try { System.out.println(remoteDevice.getName()); adapter.cancelDiscovery(); System.out.println("1"); socket = remoteDevice.createRfcommSocketToServiceRecord(UUID .fromString("00001101-0000-1000-8000-00805F9B34FB")); System.out.println("2"); socket.connect(); System.out.println("3"); PrintWriter out = new PrintWriter( new BufferedWriter(new OutputStreamWriter( socket.getOutputStream())), true); System.out.println("4"); out.println("can you se me?"); System.out.println("5"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); } }); } private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (remoteDevice == null && device.getName().contains("ZTE")) { remoteDevice = device; } show.setText(show.getText() + "\n" + device.getName() + " : " + device.getAddress()); } } }; }
服务端接收到数据通过handler传递显示:客户端扫描
运行步骤:客户端打开蓝牙,服务器端打开蓝牙,服务器端可被检测,客户端扫描,服务器端打开服务,客户端连接发送
注:本文的配对是通过系统自带的,还有注意代码里的remoteDevice
其它复杂的通信等都可在此基础上进行扩展。此为核心代码。
原文地址:http://blog.csdn.net/zhaicaixiansheng/article/details/41723615