package com.example.bt_client;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import android.app.ListActivity;
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.DialogInterface.OnCancelListener;
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.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();
private List<BluetoothDevice> _devices = new ArrayList<BluetoothDevice>();
private static final int MESSAGE_START_PROGRESS_DIALOG = 1;
private static final int MESSAGE_STOP_PROGRESS_DIALOG = 2;
private ProgressDialog _mDialog;
private boolean _refresh = false;
private TextView _refresh_but;
private Handler _mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_START_PROGRESS_DIALOG:
_mDialog = ProgressDialog.show(MainActivity.this,
"","Refresh device.\nPlease wait...");
_mDialog.setCancelable(true);
_mDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
_refresh = false;
}
});
_refresh = true;
break;
case MESSAGE_STOP_PROGRESS_DIALOG:
_refresh = false;
_mDialog.dismiss();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
_refresh_but = (TextView) findViewById(R.id.refresh_but);
_refresh_but.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(_bluetooth.isEnabled()){
_bluetooth.cancelDiscovery();
setDiscoverableTimeout(0);
_bluetooth.startDiscovery();
_mHandler.sendEmptyMessage(MESSAGE_START_PROGRESS_DIALOG);
_devices.clear();
showDevices();
}
}
});
if(!_bluetooth.isEnabled())
_bluetooth.enable();
while(!_bluetooth.isEnabled()) {
Log.d(TAG, "_bluetooth.isEnabled()");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(_foundReceiver, foundFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(_foundReceiver);
}
private BroadcastReceiver _foundReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
_devices.add(device);
showDevices();
}
};
protected void showDevices(){
List<String> list = new ArrayList<String>();
for (int i = 0, size = _devices.size(); i < size; ++i){
StringBuilder b = new StringBuilder();
BluetoothDevice d = _devices.get(i);
b.append(d.getAddress()+"\n");
b.append(d.getName());
String s = b.toString();
list.add(s);
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id){
final BluetoothDevice device = _devices.get(position);
new Thread() {
public void run() {
BluetoothSocket socket = null;
try {
socket = device.createRfcommSocketToServiceRecord(
UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));
socket.connect();
OutputStream out = socket.getOutputStream();
String m_str = "===Bluetooth test data===";
out.write(m_str.getBytes());
out.flush();
Thread.sleep(100);
out.close();
}catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
}.start();
}
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发送数据测试代码
最新推荐文章于 2024-06-19 20:59:35 发布