Skip to content

Instantly share code, notes, and snippets.

@Xiryl
Created August 12, 2021 10:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xiryl/6b0d404e05348fb18bb1c04df6af53ac to your computer and use it in GitHub Desktop.
Save Xiryl/6b0d404e05348fb18bb1c04df6af53ac to your computer and use it in GitHub Desktop.
BluetoothSDK-Fragment
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