Created
August 12, 2021 10:22
-
-
Save Xiryl/6b0d404e05348fb18bb1c04df6af53ac to your computer and use it in GitHub Desktop.
BluetoothSDK-Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CoolFragment() : BottomSheetDialogFragment() { | |
private lateinit var mService: BluetoothSDKService | |
private lateinit var binding: FragmentPopupDiscoveredLabelerDeviceBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
} | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
val view = inflater.inflate(R.layout.fragment_popup_discovered_labeler_device, container,false) | |
binding = FragmentPopupDiscoveredLabelerDeviceBinding.bind(view) | |
bindBluetoothService() | |
// Register Listener | |
BluetoothSDKListenerHelper.registerBluetoothSDKListener(requireContext(), mBluetoothListener) | |
return view | |
} | |
/** | |
* Bind Bluetooth Service | |
*/ | |
private fun bindBluetoothService() { | |
// Bind to LocalService | |
Intent( | |
requireActivity().applicationContext, | |
BluetoothSDKService::class.java | |
).also { intent -> | |
requireActivity().applicationContext.bindService( | |
intent, | |
connection, | |
Context.BIND_AUTO_CREATE | |
) | |
} | |
} | |
/** | |
* Handle service connection | |
*/ | |
private val connection = object : ServiceConnection { | |
override fun onServiceConnected(className: ComponentName, service: IBinder) { | |
val binder = service as BluetoothSDKService.LocalBinder | |
mService = binder.getService() | |
} | |
override fun onServiceDisconnected(arg0: ComponentName) { | |
} | |
} | |
private val mBluetoothListener: IBluetoothSDKListener = object : IBluetoothSDKListener { | |
override fun onDiscoveryStarted() { | |
} | |
override fun onDiscoveryStopped() { | |
} | |
override fun onDeviceDiscovered(device: BluetoothDevice?) { | |
} | |
override fun onDeviceConnected(device: BluetoothDevice?) { | |
// Do stuff when is connected | |
} | |
override fun onMessageReceived(device: BluetoothDevice?, message: String?) { | |
} | |
override fun onMessageSent(device: BluetoothDevice?) { | |
} | |
override fun onError(message: String?) { | |
} | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
// Unregister Listener | |
BluetoothSDKListenerHelper.unregisterBluetoothSDKListener(requireContext(), mBluetoothListener) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment