前一篇文章写了如何显示BLE设备,在子项点击事件中只弹出了一个吐司提示点击的是哪个模块的地址,这一篇就将它改成发送指令
public class MainActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
BluetoothDevice device= (BluetoothDevice) mListViewAdapter.getItem(i);
if (device==null)return;
Intent intent=new Intent(this,BleAtActivity.class);
intent.putExtra("name",device.getName());
intent.putExtra("address",device.getAddress());
startActivity(intent);
}
}
跳转到BleAtActivity后,在这个页面放置四个按钮用于发送,连接,断开和改名
ble_at_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/send_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/connect_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/disconnect_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_name"/>
</LinearLayout>
延续之前的代码风格,用init函数写onCreate,由于只写发送,不接收,所以代码量很少,也没什么特别的,注意BluetoothLeService不是系统的类,是需要自定义的,用到了其中的一些连接的方法和发送的方法,在后面有全文(Ctrl键+F搜索BluetoothLeService.java和BleSppGattAttributes.java,直接复制即可),我一直没有机会重写它,有机会一定要自己写一个
public class BleAtActivity extends Activity implements View.OnClickListener{
private int mLedStatus=0;
private Button button;
private Button connectButton;
private Button disconnectButton;
private Button editNameButton;
private String mAddressString;
private String mNameString;
private BluetoothLeService mBluetoothLeService;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ble_at_layout);
initView();
initData();
initEvent();
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(mServiceConnection);
mBluetoothLeService = null;
}
private void initView(){
button=findViewById(R.id.send_button);
connectButton=findViewById(R.id.connect_button);
disconnectButton=findViewById(R.id.disconnect_button);
editNameButton=findViewById(R.id.edit_name);
}
private void initData(){
mAddressString =getIntent().getStringExtra("address");
mNameString=getIntent().getStringExtra("name");
button.setText("开/关");
connectButton.setText("连接");
disconnectButton.setText("未连接");
editNameButton.setText("改名");
}
private void initEvent(){
button.setOnClickListener(this);
connectButton.setOnClickListener(this);
disconnectButton.setOnClickListener(this);
editNameButton.setOnClickListener(this);
Intent intent=new Intent(this,BluetoothLeService.class);
bindService(intent,mServiceConnection,BIND_AUTO_CREATE);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.send_button:
if (mLedStatus==0){
sendAt(