0
Android Developersサイトのドキュメントに従ってBluetooth LE機能を持つ基本的なAndroid Appを設定しようとしています。私はスキャンを開始しているところに到達することができますが、テストするために使用しているペリフェラルは見つけられません。コードはそのままです:Android Bluetooth Low Energy Not Finding Devices
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Bind(R.id.button) Button mButton;
@Bind(R.id.listView) ListView mListView;
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning = true;
private Handler mHandler;
private static final long SCAN_PERIOD = 10000;
BluetoothLeScanner mBluetoothAdapterBluetoothLeScanner;
ArrayList<String> bluetoothArray = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mButton.setOnClickListener(this);
Context mContext = getBaseContext();
final BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
mBluetoothAdapterBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1900);
}
}
@Override
public void onClick(View view) {
scanLeDevice(mScanning);
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// 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(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("test" ,device.getName());
}
});
}
};
}