作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
public class DeviceScanActivity extends AppCompatActivity/*ListActivity*/ {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new Handler();
//mSend=new BluetoothSendRecv(cntxt);
mActvty= this.getParent();
visible = this.getIntent();
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
mBluetoothManager =(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null ) {
Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
finish();
return;
}
if( !mBluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 1);
}
if( !mBluetoothAdapter.isDiscovering()) {
Intent discoverableIntent =
new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
// Construct the data source
ArrayList<ViewHolder> arrayOfUsers = new ArrayList<ViewHolder>();
// Create the adapter to convert the array to views
adapter = new UsersAdapter(this, arrayOfUsers);
cntxt=this.getApplicationContext();
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ViewHolder entry= (ViewHolder) parent.getAdapter().getItem(position);
mAddress = entry.deviceAddress;
Toast.makeText(cntxt, mAddress, Toast.LENGTH_SHORT).show();
Intent i = new Intent(cntxt, BluetoothLeService.class);
cntxt.startService(i);
bindService(visible, mConnection, BIND_AUTO_CREATE);; //if checked, start service
//final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
//mBluetoothService.mBluetoothDeviceAddress=address;
//mBluetoothService.mBluetoothManager=mBluetoothManager;
//mBluetoothService.mBluetoothAdapter = mBluetoothAdapter;
//mBluetoothService.mBluetoothGatt.connect();
/*mBluetoothService.mBluetoothGatt = */
//mSend.mBluetoothGatt=device.connectGatt(mActvty/*cntxt*/, false, mSend.mGattCallback);
//mSend.mBluetoothDeviceAddress=address;
//mSend.mBluetoothManager=mBluetoothManager;
//mSend.mBluetoothAdapter = mBluetoothAdapter;
//mSend.mBluetoothGatt.connect();
//mBluetoothService.mBluetoothGatt=mBluetoothGatt;
//Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
}});
ViewHolder newUser2 = new ViewHolder("adtv2","vvg2");
adapter.add(newUser2);
}
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
//Toast.makeText(Client.this, "Service is disconnected", 1000).show();
mBounded = false;
mBluetoothService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//Toast.makeText(Client.this, "Service is connected", 1000).show();
mBounded = true;
BluetoothLeService.LocalBinder mLocalBinder = (BluetoothLeService.LocalBinder)service;
Toast.makeText(cntxt, "Mithun", Toast.LENGTH_SHORT).show();
mBluetoothService = mLocalBinder.getService();
if (!mBluetoothService.initialize()) {
//Log.e(TAG, "Unable to initialize Bluetooth");
finish();
}
// Automatically connects to the device upon successful start-up
// initialization.
mBluetoothService.connect(mAddress);
}
};
创建第一个蓝牙管理器。从创建蓝牙适配器。然后调用 RequestPermission。然后调用 scanLeDevice。蓝牙扫描完成“周期”秒。结果存储在 ArrayList 中。为 ArrayList 和 View 创建适配器。结果显示在 ListView 中。单击列表中的每个项目时,将调用 onItemClick 方法。我们通过适配器取回发现的设备特征。为服务创建 Intent。向服务传递 mConnection 方法。但是没有调用 onServiceConnected 方法,因为没有打印“Mithun”。
最佳答案
你搞砸了 Intent
。应该是。
Intent i = new Intent(cntxt, BluetoothLeService.class);
cntxt.startService(i);
bindService(i, mConnection, BIND_AUTO_CREATE);
此外,如果此 Service
仅服务于 Activity
,则您不必使用 startService()
。有界服务和启动服务是不同的。阅读Bound services 并决定您是需要 Started Service 还是 Bounded Service。而服务可以同时启动和绑定(bind)。您可以在上面的链接中阅读更多信息。
关于android - 为什么不调用 mConnection.onServiceConnected 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51067415/
public class DeviceScanActivity extends AppCompatActivity/*ListActivity*/ { @Override public vo
我是一名优秀的程序员,十分优秀!