Mục tiêu: Tạo ứng dụng Android phát hiện tên và địa chỉ thiết bị BT trong phạm vi và gửi giá trị của chúng đến dịch vụ web. Thiết bị BT chưa được liên kết trước đó với thiết bị chủ, tôi chỉ muốn thăm dò mọi thứ khi tôi đi bộ.Phát hiện thiết bị Bluetooth trong Android - startDiscovery()
Những gì tôi đã làm:
- Các vẩy có lỗ trên tài liệu.
- Đã triển khai phiên bản cục bộ của bộ điều hợp BT của thiết bị chủ.
- Đã triển khai thông báo để bật BT nếu không bật.
- Người nhận phát sóng đã đăng ký và Mục đích để phân tích cú pháp
ACTION_FOUNDs
sắp tắt của startDiscovery(). - Đã đăng ký BLUETOOTH và BLUETOOTH_ADMIN quyền trong tệp kê khai.
Mọi thứ hoạt động (như được kiểm tra bằng ghi nhật ký điều khiển gia tăng) cho đến startDiscovery()
.
thất vọng:
- startDiscovery() - Tôi cho rằng tôi đi qua này trong bối cảnh sai. Ngữ cảnh nào mà phương pháp này cần phải được đặt bên trong để hoạt động đúng?
Nếu bạn có thể làm phương pháp này hoạt động, tôi sẽ đánh giá cao sự khôn ngoan của bạn.
CẬP NHẬT - đây là phiên bản rút gọn đơn giản của mã khiến tôi đau buồn; đơn giản hóa này tóm tắt lỗi của tôi. Mã này chạy, nó không có lỗi cat.log
hoặc các lỗi khác, nó chỉ đơn giản là không cung cấp cho bất kỳ đầu ra.
package aqu.bttest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;
public class BT2Activity extends Activity {
private BluetoothAdapter mBTA;
private SingBroadcastReceiver mReceiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//register local BT adapter
mBTA = BluetoothAdapter.getDefaultAdapter();
//check to see if there is BT on the Android device at all
if (mBTA == null){
int duration = Toast.LENGTH_SHORT;
Toast.makeText(this, "No Bluetooth on this handset", duration).show();
}
//let's make the user enable BT if it isn't already
if (!mBTA.isEnabled()){
Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBT, 0xDEADBEEF);
}
//cancel any prior BT device discovery
if (mBTA.isDiscovering()){
mBTA.cancelDiscovery();
}
//re-start discovery
mBTA.startDiscovery();
//let's make a broadcast receiver to register our things
mReceiver = new SingBroadcastReceiver();
IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, ifilter);
}
private class SingBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); //may need to chain this to a recognizing function
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 Toast
String derp = device.getName() + " - " + device.getAddress();
Toast.makeText(context, derp, Toast.LENGTH_LONG);
}
}
}
}
Nó sẽ giúp bạn biết được lỗi bạn đang nhận được ... hoặc ít nhất chính xác những gì đang dẫn bạn tin rằng 'startDiscovery()' hoạt động không chính xác. –