我正在为API 18或更高版本创建一个利用BLE的应用程序。我已经实现了BluetoothAdapter.LeScanCallBack方法但是; bluetootAdppter.StartLeScan(this)'没有调用@override onLeScan(..,..,..)方法。我搜索了整个谷歌,但没有解决方案已经解决了我的问题。请帮助我 我在SamsungGrand 2和Samsung NOTE3上测试了这个..谢谢
public class DeviceScanActivity extends Activity implements BluetoothAdapter.LeScanCallback
{
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
private static final long SCAN_PERIOD = 10000;
// private LeDeviceListAdapter mLeDeviceListAdapter;
private ArrayList<BluetoothDevice> alBlDevices;
private ArrayList<String> alDevicesNames;
BluetoothDevice tmpDevice;
LinearLayout lllist;
private BluetoothManager bluetoothManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_scan);
initData();
initUI();
}
private void initData() {
bluetoothManager = (BluetoothManager) getSystemService(getApplicationContext().BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
mHandler = new Handler();
alBlDevices = new ArrayList<BluetoothDevice>();
}
private void initUI() {
lllist = (LinearLayout) findViewById(R.id.llList);
Toast.makeText(getApplicationContext(), "initUI()", Toast.LENGTH_LONG)
.show();
scanLeDevice(true);
}
private void scanLeDevice(final boolean enable) {
if (enable) {
Toast.makeText(getApplicationContext(), "START SCANNING",
Toast.LENGTH_LONG).show();
// Stops scanning after a pre-defined scan period.
/*
* mHandler.postDelayed(new Runnable() {
*
* @Override public void run() { mScanning = false;
* mBluetoothAdapter.stopLeScan(mLeScanCallback);
*
* } }, SCAN_PERIOD);
*/
mScanning = true;
mBluetoothAdapter.startLeScan(this);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(this);
}
}
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] f)
{
Toast.makeText(getApplicationContext(), "HHHHH>", Toast.LENGTH_LONG)
.show();
tmpDevice = device;
alBlDevices.add(tmpDevice);
alDevicesNames.add(tmpDevice.getName());
Toast.makeText(getApplicationContext(),
"scanning>" + tmpDevice.getName(), Toast.LENGTH_LONG).show();
runOnUiThread(new Runnable()
{
@Override
public void run() {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View tvView = inflater.inflate(R.layout.tv_single_device, null);
((TextView) tvView.findViewById(R.id.textView1))
.setText(alDevicesNames.get(alDevicesNames.size() - 1));
lllist.addView(tvView);
Log.v("Tag", tmpDevice.getName());
}
});
}
}
...
logcat的:
05-08 10:46:06.373: E/ViewSystem(7019): ViewRootImpl #2 Surface is not valid.
05-08 10:46:06.443: D/dalvikvm(7019): GC_FOR_ALLOC freed 707K, 34% free 9276K/13920K, paused 15ms, total 15ms
05-08 10:46:06.468: D/BluetoothAdapter(7019): startLeScan(): null
05-08 10:46:06.483: D/BluetoothAdapter(7019): onClientRegistered() - status=0 clientIf=4
05-08 10:46:06.528: D/libEGL(7019): loaded /vendor/lib/egl/libGLES_mali.so
05-08 10:46:06.533: I/(7019): PLATFORM VERSION : JB-MR-2
05-08 10:46:06.548: D/mali_winsys(7019): new_window_surface returns 0x3000
05-08 10:46:06.563: D/OpenGLRenderer(7019): Enabling debug mode 0
答案 0 :(得分:0)
你需要为你的onLEScan实现回调,哪个功能返回你需要做mBluetoothAdapter.startLEScan(MyBLECallback)之类的BLE设备并实现该BLE回调的行为,它就像实现了单独的线程,问候
注意:传递参数&#34;这个&#34;永远不会工作