我正在尝试将ble 4.0数据从一个活动用到其他活动,但它不适合我,因为它是实时数据。
private void displayData(String data) {
if (data != null) {
mDataField.setText(data);
}
}
我想将此数据用于下一个用于创建graphview的活动
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
d1=(TextView) findViewById(R.id.datavalue);
Intent intent = getIntent();
String data1 = intent.getStringExtra("data");
displayData(data1);
//Intent gattServiceIntent = new Intent(this, RBLService.class);
//bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
line.clear();
}
答案 0 :(得分:0)
不是在活动之间传递数据,而是只有一个Activity,显示几个片段:
然后,您将BLE管理源代码放入您的唯一活动中。
以下是此类的示例(使您的活动implement BleObject.BleListener
并使用BleObject
在其中实例化mBleObject = new BleObject(this, this)
):
public class BleObject {
private Context mContext;
private BleListener mListener;
private Handler mRssiReader;
private Handler mRssiWatchdog;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothGatt mBluetoothGatt;
// let your Activity implement this interface (and manage Fragments)
public interface BleListener {
public void deviceFound(BluetoothDevice device, int rssi);
public void updateRssi(BluetoothDevice device, int rssi);
// add more callbacks here, for example for discovered services
}
public BleObject(Context context, BleListener listener) {
mContext = context;
mListener = listener;
mRssiReader = new Handler();
mRssiWatchdog = new Handler();
}
// this method should always be called before using BleObject
public boolean checkHardware() {
BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null)
return false;
return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
}
public boolean isEnabled() {
return mBluetoothAdapter.isEnabled();
}
private BluetoothAdapter.LeScanCallback mScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
if (device == null)
return;
String address = device.getAddress();
if (!BluetoothAdapter.checkBluetoothAddress(address))
return;
mListener.deviceFound(device, rssi);
}
};
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
readPeriodicalyRssi();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
disconnect();
}
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
if (gatt == null)
return;
BluetoothDevice device = gatt.getDevice();
if (device == null)
return;
if (status == BluetoothGatt.GATT_SUCCESS) {
mListener.updateRssi(device, rssi);
}
}
};
private Runnable mWatchdogRunnable = new Runnable() {
@Override
public void run() {
mGattCallback.onReadRemoteRssi(mBluetoothGatt, -666, BluetoothGatt.GATT_SUCCESS);
}
};
private Runnable mRssiRunnable = new Runnable() {
@Override
public void run() {
if (mBluetoothGatt == null)
return;
mBluetoothGatt.readRemoteRssi();
readPeriodicalyRssi();
}
};
private void readPeriodicalyRssi() {
mRssiReader.postDelayed(mRssiRunnable, 1000);
mRssiWatchdog.removeCallbacks(mWatchdogRunnable);
mRssiWatchdog.postDelayed(mWatchdogRunnable, 10000);
}
@SuppressWarnings("deprecation")
public void startScanning() {
mBluetoothAdapter.startLeScan(mScanCallback);
}
@SuppressWarnings("deprecation")
public void stopScanning() {
mBluetoothAdapter.stopLeScan(mScanCallback);
}
public void connect(String address) {
connect(mBluetoothAdapter.getRemoteDevice(address));
}
public void connect(final BluetoothDevice device) {
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
mBluetoothGatt = device.connectGatt(mContext, true, mGattCallback);
}
});
}
public void disconnect() {
try {
mBluetoothGatt.disconnect();
} catch (Exception e) {
}
try {
mBluetoothGatt.close();
} catch (Exception e) {
}
mBluetoothGatt = null;
}
}