| | |
| | | MasterInqRoadStatus(); |
| | | break; |
| | | } |
| | | case ID_MS_BLUETOOTH_NAME: { |
| | | Document doc; |
| | | doc.Parse(value); |
| | | if (!doc.HasParseError()) { |
| | | if (doc.HasMember("bluetooth_name") && doc.HasMember("bluetooth_addr")) { |
| | | const Value& s1 = doc["bluetooth_name"]; |
| | | const Value& s2 = doc["bluetooth_addr"]; |
| | | SetRemoteBluetooth(s1.GetString(), s2.GetString()); |
| | | } else if (doc.HasMember("bluetooth_addr")) { |
| | | const Value& s = doc["bluetooth_addr"]; |
| | | SetRemoteBluetooth(NULL, s.GetString()); |
| | | } |
| | | } |
| | | break; |
| | | } |
| | | default:break; |
| | | } |
| | | } |
| | |
| | | SendMsgToMainProcIndep(ID_SM_LIGHT_EXAM_REQ, NULL); |
| | | } |
| | | |
| | | void MA_SendBlueStatus(int status) |
| | | void MA_SendBlueStatus(const char *name, const char *addr, int status) |
| | | { |
| | | StringBuffer sb; |
| | | Writer<StringBuffer> writer(sb); |
| | | |
| | | writer.StartObject(); |
| | | |
| | | if (name != NULL) { |
| | | writer.Key("bluetooth_name"); |
| | | writer.String(name); |
| | | } |
| | | if (addr != NULL) { |
| | | writer.Key("bluetooth_addr"); |
| | | writer.String(addr); |
| | | } |
| | | |
| | | writer.Key("bluetooth_status"); |
| | | writer.Int(status); |
| | | writer.EndObject(); |
| | |
| | | void MA_SendCrossingStatus(const struct crossingStatusBrief *brief); |
| | | void MA_SendCanStatus(const struct canBrief *brief); |
| | | |
| | | void MA_SendBlueStatus(int status); |
| | | void MA_SendBlueStatus(const char *name, const char *addr, int status); |
| | | |
| | | #endif //MYAPPLICATION2_COMM_IF_H |
| | |
| | | jclass cls = env->GetObjectClass(sg_obj); |
| | | jmethodID fun = env->GetMethodID(cls, "ConnectBluetooth", "(Ljava/lang/String;Ljava/lang/String;)V"); |
| | | |
| | | env->CallVoidMethod(sg_obj, fun, env->NewStringUTF(addr), env->NewStringUTF(pin)); |
| | | env->CallVoidMethod(sg_obj, fun, env->NewStringUTF(addr), (pin == NULL) ? NULL : env->NewStringUTF(pin)); |
| | | |
| | | env->DeleteLocalRef(cls); |
| | | |
| | | if (!ready_in_java_env) { |
| | | //Detach主线程 |
| | | if (sg_jvm->DetachCurrentThread() != JNI_OK) { |
| | | LOGE("%s: DetachCurrentThread() failed", __FUNCTION__); |
| | | } |
| | | } |
| | | } |
| | | |
| | | void DisconnectBluetooth(void) |
| | | { |
| | | JNIEnv *env; |
| | | bool ready_in_java_env = false; |
| | | |
| | | if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) { |
| | | // Attach主线程 |
| | | if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) { |
| | | LOGE("%s: AttachCurrentThread() failed", __FUNCTION__); |
| | | return; |
| | | } |
| | | } else { |
| | | ready_in_java_env = true; |
| | | } |
| | | |
| | | jclass cls = env->GetObjectClass(sg_obj); |
| | | jmethodID fun = env->GetMethodID(cls, "DisconnectBluetooth", "()V"); |
| | | |
| | | env->CallVoidMethod(sg_obj, fun); |
| | | |
| | | env->DeleteLocalRef(cls); |
| | | |
| | |
| | | |
| | | PlatformStatusChanged(BLUETOOTH_STATUS_EVT, &sta, 1); |
| | | } |
| | | |
| | | extern "C" |
| | | JNIEXPORT void JNICALL |
| | | Java_com_anyun_exam_lib_RemoteService_BluetoothConnected(JNIEnv *env, jobject thiz, jstring name, |
| | | jstring addr) { |
| | | // TODO: implement BluetoothConnected() |
| | | if (name != NULL && addr != NULL) { |
| | | const char *strname = env->GetStringUTFChars(name, 0); |
| | | const char *straddr = env->GetStringUTFChars(addr, 0); |
| | | |
| | | uint8_t data[128]; |
| | | strcpy((char *)data, strname); |
| | | strcpy((char *)data + 64, straddr); |
| | | |
| | | env->ReleaseStringUTFChars(name, strname); |
| | | env->ReleaseStringUTFChars(addr, straddr); |
| | | |
| | | PlatformStatusChanged(BLUETOOTH_STATUS_EVT, data, 128); |
| | | } else if (addr != NULL) { |
| | | const char *straddr = env->GetStringUTFChars(addr, 0); |
| | | |
| | | uint8_t data[64]; |
| | | strcpy((char *)data, straddr); |
| | | |
| | | env->ReleaseStringUTFChars(addr, straddr); |
| | | |
| | | PlatformStatusChanged(BLUETOOTH_STATUS_EVT, data, 64); |
| | | } else { |
| | | uint8_t sta = 3; |
| | | PlatformStatusChanged(BLUETOOTH_STATUS_EVT, &sta, 1); |
| | | } |
| | | } |
| | |
| | | void PlayRing(void); |
| | | void SendToBluetooth(const uint8_t *data, int length); |
| | | void ConnectToBluetooth(const char *addr, const char *pin); |
| | | void DisconnectBluetooth(void); |
| | | |
| | | #endif //RTKBASESTATION_NATIVE_LIB_H |
| | |
| | | |
| | | #define DEBUG(fmt, args...) LOGD("<platform> <%s>: " fmt, __func__, ##args) |
| | | |
| | | using namespace std; |
| | | |
| | | struct platformSocket { |
| | | char domain_name[32]; |
| | | int port; |
| | |
| | | |
| | | static struct gpsBrief gbf; |
| | | static struct rtkBrief rbf; |
| | | |
| | | static bool btEnable = false, btConnected = false; |
| | | static char btAddr[64] = {0}; |
| | | static char *btPin = NULL; |
| | | |
| | | static void AddEvnet(uint32_t event, const uint8_t *data, int length); |
| | | static struct event_t * FetchEvent(void); |
| | |
| | | } |
| | | } |
| | | if (events & BLUETOOTH_STATUS_EVT) { |
| | | DEBUG("BLUETOOTH_STATUS_EVT %d", data[0]); |
| | | DEBUG("BLUETOOTH_STATUS_EVT"); |
| | | uint8_t sta; |
| | | |
| | | if (data[0] == 3) { |
| | | if (length == 128) { |
| | | MA_SendBlueStatus((char *)data, (char *)data+64, 3); |
| | | sta = 3; |
| | | } else if (length == 64) { |
| | | MA_SendBlueStatus(NULL, (char *)data, 3); |
| | | sta = 3; |
| | | } else { |
| | | MA_SendBlueStatus(NULL, NULL, data[0]); |
| | | sta = data[0]; |
| | | } |
| | | |
| | | if (sta == 3) { |
| | | // Connected |
| | | btConnected = true; |
| | | ParseMcuInit(); |
| | | ConfigRTKModuleLater(); |
| | | |
| | | PlayTTS("蓝牙连接", NULL); |
| | | } else if (data[0] == 2) { |
| | | } else if (sta == 2) { |
| | | // Disconnect |
| | | btConnected = false; |
| | | PlayTTS("蓝牙断开", NULL); |
| | | } else if (data[0] == 1) { |
| | | } else if (sta == 1) { |
| | | // Open |
| | | ConnectToBluetooth("00:1B:35:16:20:4A", "3800"); |
| | | btEnable = true; |
| | | |
| | | if (strlen(btAddr) > 0) { |
| | | ConnectToBluetooth(btAddr, NULL); |
| | | } |
| | | // ConnectToBluetooth("00:1B:35:16:20:4A", "5516"); |
| | | // ConnectToBluetooth("00:1B:35:16:20:4A", "3800");`` |
| | | // ConnectToBluetooth("00:1D:43:9A:E0:79", "1900"); |
| | | // ConnectToBluetooth("DESKTOP-IE9V7U8", "0000"); |
| | | PlayTTS("蓝牙启动", NULL); |
| | | } else { |
| | | // Close |
| | | btEnable = false; |
| | | btConnected = false; |
| | | PlayTTS("蓝牙关闭", NULL); |
| | | } |
| | | } |
| | |
| | | memcpy(defaultMcuRom.rom, rom, length); |
| | | } |
| | | } |
| | | |
| | | void SetRemoteBluetooth(const char *name, const char *addr) |
| | | { |
| | | strcpy(btAddr, addr); |
| | | |
| | | if (btConnected) { |
| | | DisconnectBluetooth(); |
| | | } |
| | | |
| | | /*string csname = name; |
| | | if (csname != NULL) { |
| | | if (csname.find("AKS3A_TEST") != string::npos) { |
| | | btPin = "1111"; |
| | | } else if (csname.find("AKS3A_") != string::npos && csname.size() >= 8) { |
| | | int a = stoi(csname.substr(csname.size()-8, 4)); |
| | | int b = stoi(csname.substr(csname.size()-4, 4)); |
| | | } |
| | | }*/ |
| | | |
| | | if (btEnable) { |
| | | ConnectToBluetooth(addr, NULL); |
| | | } |
| | | } |
| | |
| | | void StopRtkDownload(void); |
| | | |
| | | void LoadDefaultMcuRom(const char *verCode, const uint8_t *rom, int length); |
| | | void SetRemoteBluetooth(const char *name, const char *addr); |
| | | |
| | | #endif //RTKDRIVERTEST_PLATFORM_H |
| | |
| | | private Bluetooth mBluetooth = null; |
| | | private BluetoothChatService mChatService = null; |
| | | private String mConnectedDeviceName = null; |
| | | private String mConnectedDeviceAddr = null; |
| | | |
| | | private String mTargetBluetooth = null; |
| | | private String mTargetBluetoothAddr = null; |
| | | private boolean mDiscoveryBluetooth = false; |
| | |
| | | } |
| | | } |
| | | |
| | | public void DisconnectBluetooth() |
| | | { |
| | | handlerConnectBluetooth.removeCallbacks(runnableConnectBluetooth); |
| | | if (mChatService != null) { |
| | | mChatService.stop(); |
| | | } |
| | | } |
| | | |
| | | Handler handlerConnectBluetooth = new Handler(); |
| | | Runnable runnableConnectBluetooth = new Runnable() { |
| | | @Override |
| | |
| | | switch (msg.arg1) { |
| | | case BluetoothChatService.STATE_CONNECTED: |
| | | Log.d(TAG, "蓝牙已连接"); |
| | | BluetoothStatusChange(3); |
| | | // BluetoothStatusChange(3); |
| | | BluetoothConnected(mConnectedDeviceName, mConnectedDeviceAddr); |
| | | break; |
| | | case BluetoothChatService.STATE_CONNECTING: |
| | | Log.d(TAG, "蓝牙连接中..."); |
| | |
| | | mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME); |
| | | Log.d(TAG, "MESSAGE_DEVICE_NAME: " + mConnectedDeviceName); |
| | | break; |
| | | case Constants.MESSAGE_DEVICE_ADDRESS: |
| | | mConnectedDeviceAddr = msg.getData().getString(Constants.DEVICE_ADDRESS); |
| | | Log.d(TAG, "MESSAGE_DEVICE_ADDRESS: " + mConnectedDeviceAddr); |
| | | break; |
| | | case Constants.MESSAGE_TOAST: |
| | | break; |
| | | default:break; |
| | |
| | | public native void MainProcBinMsgEntry(int cmd, byte []data, int length); |
| | | public native void UpgradeMcu(String vercode, byte []rom); |
| | | public native void TextSpeakEnd(int id); |
| | | public native void BluetoothConnected(String name, String addr); |
| | | public native void BluetoothStatusChange(int status); |
| | | public native void BluetoothDataComeIn(byte []data, int length); |
| | | } |
| | |
| | | |
| | | public class Bluetooth extends BroadcastReceiver { |
| | | private static final String TAG = Bluetooth.class.getCanonicalName(); |
| | | public static final String DEFAULT_BT_PASSWORD = "0000"; |
| | | public static final String DEFAULT_BT_PASSWORD = "1111"; |
| | | |
| | | private static Bluetooth instance = null; |
| | | private Context context; |
| | | private BluetoothAdapter mBtAdapter = null; |
| | | private Handler mHandler; |
| | | private String pin = DEFAULT_BT_PASSWORD; |
| | | private String pin = null; |
| | | private String mName = null; |
| | | |
| | | public static Bluetooth getInstance(Context context, Handler handler) { |
| | |
| | | filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); |
| | | filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); |
| | | filter.addAction("android.bluetooth.device.action.PAIRING_REQUEST"); |
| | | |
| | | filter.setPriority(1000); |
| | | |
| | | context.registerReceiver(this, filter); |
| | | } |
| | | |
| | |
| | | Log.d(TAG, "匹配请求"); |
| | | // Get the BluetoothDevice object from the Intent |
| | | BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
| | | |
| | | String name = device.getName(); |
| | | |
| | | if (pin == null && name != null && name.contains("AKS3A_") && name.length() >= 8) { |
| | | char [] sn = new char[8]; |
| | | name.getChars(name.length() - 8, name.length(), sn, 0); |
| | | int a = ((sn[0] - '0') * 10 + sn[1] - '0') * 100 + (sn[2] - '0') * 10 + sn[3] - '0'; |
| | | int b = ((sn[4] - '0') * 10 + sn[5] - '0') * 100 + (sn[6] - '0') * 10 + sn[7] - '0'; |
| | | |
| | | a = (a * b * 1978) % 10000; |
| | | |
| | | pin = String.format("%02d", a/100) + String.format("%02d", a%100); |
| | | } |
| | | |
| | | if (pin == null) { |
| | | pin = DEFAULT_BT_PASSWORD; |
| | | } |
| | | |
| | | Log.d(TAG, "匹配吗 " + pin); |
| | | /*if (device.getName().equals(BLUETOOTH_OBD_NAME) || device.getName().equals(BLUETOOTH_TARGET2_NAME)) */{ |
| | | try { |
| | | ClsUtils.setPairingConfirmation(device.getClass(), device, true); |
| | |
| | | |
| | | ClsUtils.setPin(device.getClass(), device, pin); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | Log.e(TAG, e.getMessage()); |
| | | } |
| | | } |
| | | } else if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(action)) { |
| | |
| | | * This class does all the work for setting up and managing Bluetooth |
| | | * connections with other devices. It has a thread that listens for |
| | | * incoming connections, a thread for connecting with a device, and a |
| | | * thread for performing data transmissions when connected. |
| | | * thread for amperforming data transmisspions when connected. |
| | | */ |
| | | public class BluetoothChatService { |
| | | // Debugging |
| | |
| | | msg.setData(bundle); |
| | | mHandler.sendMessage(msg); |
| | | |
| | | msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_ADDRESS); |
| | | bundle = new Bundle(); |
| | | bundle.putString(Constants.DEVICE_ADDRESS, device.getAddress()); |
| | | msg.setData(bundle); |
| | | mHandler.sendMessage(msg); |
| | | |
| | | setState(STATE_CONNECTED); |
| | | |
| | | // Start the thread to manage the connection and perform transmissions |
| | |
| | | public static final int MESSAGE_BLUETOOTH_STATUS = 6; |
| | | public static final int MESSAGE_BLUETOOTH_FOUND = 7; |
| | | public static final int MESSAGE_BLUETOOTH_DISCOVERY_FINISHED = 8; |
| | | public static final int MESSAGE_DEVICE_ADDRESS = 9; |
| | | |
| | | // Key names received from the BluetoothChatService Handler |
| | | public static final String DEVICE_NAME = "device_name"; |