private synchronized int disconnectMasSession(Instance instance) { int result = FAIL; if (instance == null || !instance.isMasConnected()) { return result; } result = disconnectMasNative(instance.getInstanceId()); if (result == SUCCESS) { instance.onDeviceDisconnecting(instance.getDevice()); return PENDING; } else { instance.onDeviceDisconnected(instance.getDevice()); return SUCCESS; } }
// @Override public void handleMessage(Message msg) { Instance instance; ArrayList<Instance> instances; int result = MAP.RESULT_ERROR; String addr; BluetoothDevice device = null; log("message received: " + msg.what); switch (msg.what) { case MAP_SERVER_ENABLE_CNF: /*to do: set state*/ setState(BluetoothMap.STATE_ENABLED); sendMessage(obtainMessage(MAP_REGISTER_SERVER)); onStateChanged(BluetoothMap.STATE_ENABLED); break; case MAP_SERVER_DISABLE_CNF: /*to do: set state*/ mManager.removeAllInstances(); setState(BluetoothMap.STATE_DISABLED); onStateChanged(BluetoothMap.STATE_DISABLED); deinitServer(); break; case MAP_SERVER_REGISTER_CNF: instance = mManager.getInstanceById(msg.arg1); if (msg.arg2 >= 0 && instance != null) { instance.onInstanceRegistered(); } else { mManager.removeInstance(msg.arg1); } break; case MAP_SERVER_DEREGISTER_CNF: instance = mManager.getInstanceById(msg.arg1); if (instance != null) { instance.onInstanceDeregistered(); } mManager.removeInstance(msg.arg1); break; case MAP_SERVER_CONNECT_IND: instance = mManager.getInstanceById(msg.arg1); device = mAdapter.getRemoteDevice((String) msg.obj); if (instance != null) { instance.onDeviceConnected(device); } else { log("invalid instance ID is received"); return; } mNotification.createNotification( BluetoothMapNotification.ALERT_TYPE_CONNECT, device, true); break; case MAP_SERVER_DISCONNECT_CNF: case MAP_SERVER_DISCONNECT_IND: instance = mManager.getInstanceById(msg.arg1); device = mAdapter.getRemoteDevice((String) msg.obj); if (instance != null) { instance.onDeviceDisconnected(device); } else { log("invalid instance ID is received:" + msg.arg1); } if (mManager.getState(device) == BluetoothMap.STATE_DISCONNECTED) { mNotification.removeNotification( BluetoothMapNotification.ALERT_TYPE_CONNECT, device); } if (mState == BluetoothMap.STATE_DISABLING) { removeMessages(MAP_DISCONNECT_INSTANCE_TIMEOUT, instance); disable(false); } break; case MAP_SERVER_SET_FOLDER: instance = mManager.getInstanceById(msg.arg1); if (msg.obj == null || instance == null) { log("SetFolderRequest or instance is null"); return; } SetFolderRequest setFolderReq = (SetFolderRequest) msg.obj; result = instance.setFolder(setFolderReq); setFolderResponse(setFolderReq.getAddress(), msg.arg1, result); break; case MAP_SERVER_UPDATE_INBOX: instance = mManager.getInstanceById(msg.arg1); if (msg.obj == null || instance == null) { log("address or instance is null"); return; } addr = (String) msg.obj; result = instance.updateInbox(); updateInboxResponse(addr, msg.arg1, result); break; case MAP_SERVER_GET_MESSAGE_LIST: instance = mManager.getInstanceById(msg.arg1); if (msg.obj == null || instance == null) { log("address is null or instance is null"); return; } MessageListRequest msgListReq = (MessageListRequest) msg.obj; MessageListObject msgListRsp = instance.getMessagelist(msgListReq); addr = msgListReq.getAddress(); getMessageListResponse(addr, msg.arg1, MAP.RESULT_OK, msgListRsp); break; case MAP_SERVER_GET_FOLDER_LIST: instance = mManager.getInstanceById(msg.arg1); if (msg.obj == null || instance == null) { log("address is null or instance is null"); return; } FolderListObject[] fodlerlistrsp = null; FolderListRequest folderListReq = (FolderListRequest) msg.obj; addr = folderListReq.getAddress(); device = mAdapter.getRemoteDevice(addr); if (device != null && device.equals(instance.getDevice())) { fodlerlistrsp = instance.getFolderlist(folderListReq); } getFolderListResponse(addr, msg.arg1, MAP.RESULT_OK, fodlerlistrsp); break; case MAP_SERVER_GET_MESSAGE: instance = mManager.getInstanceById(msg.arg1); if (msg.obj == null || instance == null) { log("address is null or instance is null"); return; } MessageRequest messagereq = (MessageRequest) msg.obj; BMessageObject messagersp = instance.getMessage(messagereq); if (messagersp != null) { result = MAP.RESULT_OK; } addr = messagereq.getAddress(); getMessageResponse(addr, msg.arg1, result, messagersp); /* if (messagersp != null) { messagersp.reset(); } */ break; case MAP_SERVER_SET_NOTIFICATION: instance = mManager.getInstanceById(msg.arg1); if (msg.obj == null || instance == null) { log("address is null or instance is null"); return; } result = MAP.RESULT_OK; setNotificationRegResponse((String) msg.obj, msg.arg1, result); break; case MAP_SERVER_SEND_REPORT_CNF: instance = mManager.getInstanceById(msg.arg1); if (msg.obj == null || instance == null) { log("address is null or instance is null"); return; } mEventManager.onEventSentCompleted(); break; case MAP_SERVER_AUTHORIZE_IND: // TODO: launch an alert to notify user that a device is trying to access local // resource if (msg.obj == null) { log("address is null"); return; } device = mAdapter.getRemoteDevice((String) msg.obj); int state = mNotification.getDeviceState(device); if (state == BluetoothMap.STATE_NONE) { mNotification.createNotification( BluetoothMapNotification.ALERT_TYPE_AUTHORIZE, device, true); } else if (state == BluetoothMap.STATE_CONNECTED) { // already connected authorizeResponse(device, true); } else if (state == BluetoothMap.STATE_AUTHORIZING) { log("the device is authorizing"); // do nothing } else { log("unexpected state : " + state); authorizeResponse(device, false); } break; case MAP_SERVER_PUSH_MESSAGE: instance = mManager.getInstanceById(msg.arg1); if (msg.obj == null || instance == null) { log("address is null or instance is null"); return; } BMessageObject bMessageobject = instance.getBMessageObject(); bMessageobject.reset(); if (parseMessage((String) msg.obj, msg.arg1, bMessageobject) == false) { result = MAP.RESULT_ERROR; } else { if (instance.pushMessage(bMessageobject)) { result = MAP.RESULT_OK; } else { result = MAP.RESULT_ERROR; } } sendMessageResponse((String) msg.obj, msg.arg1, result, bMessageobject.getHandle()); break; /* case MAP_SERVER_DELETE_MESSAGE: instance = mManager.getInstanceById(msg.arg1); if (msg.obj == null || instance == null) { log("address is null or instance is null"); return; } //BMessageObject bMessageobject = instance.getBMessageObject(); //sendMessageResponse((String) msg.obj, msg.arg1 ,bMessageobject); if(instance.deleteMessage(msg.arg1, msg.arg2)){ result = MAP.RESULT_OK; } else { result = MAP.RESULT_ERROR; } setMessageStatusResponse((String)msg.obj, msg.arg1, result); break; */ case MAP_SERVER_SET_MESSAGE_STATUS: instance = mManager.getInstanceById(msg.arg1); if (msg.obj == null || instance == null) { log("address is null or instance is null"); return; } StatusSwitchRequest req = (StatusSwitchRequest) msg.obj; if (instance.setMessageStatus(req)) { result = MAP.RESULT_OK; } else { result = MAP.RESULT_ERROR; } addr = req.getAddress(); setMessageStatusResponse(addr, msg.arg1, result); break; case MAP_SERVER_MNS_DISCONNCET_CNF: device = mAdapter.getRemoteDevice((String) msg.obj); instances = mManager.getInstanceByDevice(device); for (int i = 0; i < instances.size(); i++) { instances.get(i).deregisterCallback(); } if (mState == BluetoothMap.STATE_DISABLING) { disable(false); } break; case MAP_SERVER_MNS_CONNECT_CNF: if (msg.arg1 == MAP.RESULT_ERROR) { log("fail to set up mns connection"); } else { device = mAdapter.getRemoteDevice((String) msg.obj); instances = mManager.getInstanceByDevice(device); for (int i = 0; i < instances.size(); i++) { instances.get(i).registerCallback(mEventManager); } } break; case MAP_REGISTER_SERVER: if (NetworkUtil.isPhoneRadioReady()) { registerServer(); } else { mRegSrvCnt++; if (mRegSrvCnt < 10) { sendMessageDelayed( obtainMessage(MAP_REGISTER_SERVER), REGISTER_SERVER_DELAY_DURATION); } else { log("Network is still not ready after 10s, so phone radio maybe is in error"); } } break; case MAP_DISCONNECT_INSTANCE_TIMEOUT: instance = (Instance) msg.obj; if (instance != null) { instance.onDeviceDisconnected(instance.getDevice()); if (mState == BluetoothMap.STATE_DISABLING) { disable(false); } } break; default: log("Unrecognized message"); } }