package com.example.bt_server;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
/*
* 注意:手机需要设置上“开放检测”才可以。
* */
public class MainActivity extends Activity {
public static final String PROTOCOL_SCHEME_L2CAP = "btl2cap";
public static final String PROTOCOL_SCHEME_RFCOMM = "btspp";
public static final String PROTOCOL_SCHEME_BT_OBEX = "btgoep";
public static final String PROTOCOL_SCHEME_TCP_OBEX = "tcpobex";
private static final String TAG = MainActivity.class.getSimpleName();
private BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();
private long _mainThreadId = Thread.currentThread().getId();
private BluetoothServerSocket _serverSocket;
private boolean _serverFlag = true;
private TextView log = null;
private String logStr = "";
private void viewDispLog(final String str){
if(Thread.currentThread().getId() == _mainThreadId){
logStr += "m:"+str;
log.setText(logStr);
}else{
runOnUiThread(new Runnable() {
@Override
public void run() {
logStr += "c:"+str;
log.setText(logStr);
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
log = (TextView)findViewById(R.id.log);
if(!_bluetooth.isEnabled())
_bluetooth.enable();
while(!_bluetooth.isEnabled()) {
Log.d(TAG, "_bluetooth.isEnabled()");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
viewDispLog("_bluetooth enable\n");
setDiscoverableTimeout(0);
viewDispLog("_discovery start\n");
_bluetooth.startDiscovery();
viewDispLog("_server thread start\n");
_serverThread.start();
}
protected void onDestroy() {
super.onDestroy();
shutdownThread();
}
private void shutdownThread() {
_serverFlag = false;
if(_serverThread != null){
_serverThread.interrupt();
}
if(_serverSocket != null){
try {
_serverSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
_serverSocket = null;
}
_bluetooth.disable();
}
private Thread _serverThread = new Thread() {
public void run() {
try {
_serverSocket = _bluetooth.listenUsingRfcommWithServiceRecord(
PROTOCOL_SCHEME_RFCOMM,
UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));
while(_serverFlag){
viewDispLog("---accept---\n");
BluetoothSocket socket = _serverSocket.accept();
if (socket != null) {
viewDispLog("---input data---\n");
InputStream inputStream = socket.getInputStream();
int read = -1;
final byte[] bytes = new byte[2048];
read = inputStream.read(bytes);
if(read > -1){
String s = new String(bytes,0,read);
viewDispLog("recv#\n"+s+"\n");
}
inputStream.close();
}
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if(_serverSocket != null){
try {
_serverSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
_serverSocket = null;
}
}
};
public void setDiscoverableTimeout(int timeout) {
BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();
try {
Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class);
setDiscoverableTimeout.setAccessible(true);
Method setScanMode =BluetoothAdapter.class.getMethod("setScanMode", int.class,int.class);
setScanMode.setAccessible(true);
setDiscoverableTimeout.invoke(adapter, timeout);
setScanMode.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,timeout);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Bluetooth的接收数据测试代码
最新推荐文章于 2023-08-29 09:54:17 发布