我正在尝试编写一个应用程序,搜索可见的蓝牙Android设备并向每个设备发送文件!
到目前为止,我在网上搜索了this文章。在文章中,它搜索设备并按如下方式连接到它们:(请注意,我记录了我的其他手机蓝牙地址并硬编码了它的地址以便测试我的应用程序)
private static final int REQUEST_ENABLE_BT = 1;
BluetoothAdapter mBluetoothAdapter;
private ArrayList<BluetoothDevice> mDevicesArrayAdapter = new ArrayList<>();
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mDevicesArrayAdapter.add(device);
if (device.getAddress().equals("4C:A5:6D:00:02:C0")){
Thread connect = new ConnectThread(device);
connect.run();
Log.e("found my phone", "now");
}
Log.e("Bluetooth Device", device.getName()+" "+device.getAddress());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
turnBluetoothOn();
}
private void turnBluetoothOn() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "This device does not support bluetooth!!", Toast.LENGTH_LONG).show();
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
searchForBluetoothDevices();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == REQUEST_ENABLE_BT){
searchForBluetoothDevices();
}
}
private void searchForBluetoothDevices() {
boolean discovery = mBluetoothAdapter.startDiscovery();
if(discovery) {
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
} else {
Toast.makeText(this, "Error in starting discovery", Toast.LENGTH_LONG).show();
}
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(UUID.fromString("f68e0858-9097-44bc-9a9c-0c7e342998d5"));
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
Log.e("connected", mmDevice.getName());
} catch (IOException connectException) {
connectException.printStackTrace();
try {
mmSocket.close();
} catch (IOException closeException) {
closeException.printStackTrace();
}
return;
}
manageConnectedSocket(mmSocket);
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
private void manageConnectedSocket(BluetoothSocket mmSocket) {
Thread connected = new ConnectedThread(mmSocket);
connected.run();
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.e("started", "sharing");
FileInputStream fis = (FileInputStream) getResources().openRawResource(
R.raw.style);
byte[] buffer = new byte[1024];
int bytes;
int read;
do {
try {
read = fis.read(buffer, 0, 1024);
mmOutStream.write(buffer);
} catch (IOException e) {
break;
}
} while (read != 0);
}
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
任何人都可以帮助我吗?有什么想法让它发挥作用?我非常绝望!我到处搜索,但似乎无法在蓝牙上找到任何有用的东西! 任何人都可以介绍一个示例开源应用程序来使用蓝牙吗?