在用户界面上显示值更快速的Android JAVA
我正在开发Android JAVA应用程序,它将BLE与另一台设备结合使用。 所以有两页。一个是用户正在尝试扫描设备并连接。(DeviceControlActivity.java)
如果连接完成,那么用户可以看到主页(MainActivity.java)。 我想马上显示一个值,但需要1~2秒
我认为有一种方法可以在显示主要活动之前获得一个值
- 顺序
- 用户可连接设备(DeviceControlActivity.java)
- 广播更新->;从characteristic(BluetoothLeService.java)获取一个值
- 主要活动->;onCreate->;显示来自BroadcastReceiver(MainActivity.java)的值
设备控制。爪哇
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
if (!mBluetoothLeService.initialize()) {
Log.e(TAG, "Unable to initialize Bluetooth");
finish();
}
// Automatically connects to the device upon successful start-up initialization.
Log.w("이게", mDeviceAddress.toString());
mBluetoothLeService.connect(mDeviceAddress);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
}
};
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
mConnected = true;
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
mConnected = false;
//resultText.setBackgroundResource(R.drawable.bleno);
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
resultText.setText(R.string.bluetoothsucces);
SharedPreferences sharedPreferences = getSharedPreferences("token", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("device", mDeviceName);
editor.putString("address", mDeviceAddress);
editor.commit();
checkblue = true;
// Show all the supported services and characteristics on the user interface.
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
Log.w("test", intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
if (intent.getStringExtra(BluetoothLeService.EXTRA_DATA).contains("}")) {
startActivity(new Intent(getApplication(), MainActivity.class));
mainDialog.hide();
}
}
}
};
// If a given GATT characteristic is selected, check for supported features. This sample
// demonstrates 'Read' and 'Notify' features. See
// http://d.安卓.com/reference/安卓/bluetooth/BluetoothGatt.html for the complete
// list of supported characteristic features.
/* private final ExpandableListView.OnChildClickListener servicesListClickListner =
new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic =
mGattCharacteristics.get(groupPosition).get(childPosition);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
mBluetoothLeService.send("{svr}mqtt://52.79.85.42:1883#");
mBluetoothLeService.send("{wifi_id}blueshift#");
mBluetoothLeService.send("{wifi_pw}blueshift09#");
//mBluetoothLeService.readCharacteristic(characteristic);
}
return true;
}
return false;
}
};*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect_result);
bContext = this;
final Intent intent = getIntent();
mainDialog = new MainDialog(this);
mainDialog.setCancelable(false);
mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);
addCateDialog = new BluetoothDialogResult(this);
addCateDialog.setCancelable(false);
deviceSearch = findViewById(R.id.deviceSearch);
resultText = findViewById(R.id.maintext);
mainDialog.show();
deviceSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addCateDialog.show();
}
});
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
public void run() {
if (checkblue == false) {
mainDialog.hide();
deviceSearch.setText("");
resultText.setText(".");
}
}
}, 10000);
Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
if (mBluetoothLeService != null) {
final boolean result = mBluetoothLeService.connect(mDeviceAddress);
Log.d(TAG, "Connect request result=" + result);
}
}
蓝牙服务。爪哇
private void broadcastUpdate(final String action,
final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
String batteryLevelString ="batteryLevel:";
// This is special handling for the Heart Rate Measurement profile. Data
// parsing is carried out as per profile specifications.
if(UUID_BATTERY_LEVEL.equals(characteristic.getUuid())) {
final byte[] batteryLevel = characteristic.getValue();
if (batteryLevel[0] == 49 && batteryLevel[1] == 48 && batteryLevel[2] == 48)
{
batteryLevelString = batteryLevelString.concat("100");
intent.putExtra(EXTRA_DATA, String.valueOf(batteryLevelString));
}
else{
String aString = new String(batteryLevel);
batteryLevelString = batteryLevelString.concat(aString);
intent.putExtra(EXTRA_DATA, String.valueOf(batteryLevelString));
}
// { 57,49, 0} {31, 30, 30}
}
else {
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
String temp = new String(data);
temp = temp.substring(4, temp.length());
intent.putExtra(EXTRA_DATA, temp);
}
}
sendBroadcast(intent);
}
主要活动。爪哇
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
if (intent.getStringExtra(BluetoothLeService.EXTRA_DATA).contains("batteryLevel:")) {
batterylevel = "";
batterylevel += intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
batterylevel = batterylevel.substring(13);
txtBatteryLevel.setText(batterylevel + "%");
}
}
共 (0) 个答案