question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problems with read subscription

See original GitHub issue

Before starting this I want to shortly mention two things. Thanks for creating and maintaining this library and I’m pretty sure the fault is on my side. I just don’t know what I’m doing wrong 🙈

Mobile Device Environment

  • Device: Android
  • OS: API 29

Application Environment Provide information about your development environment:

  • React Native version: 0.63.4
  • RN Bluetooth Classic version: 1.60.0-rc.17

Describe the bug I have an app that can connect to python client on a laptop and send messages but does only receives the messages from the python client once the connection is closed. Additionally I get a weird error when I try to remove the read subscription. I’m not sure if both problems are related.

Below you can find the two important files. When I run readSubscription.remove() I receive the following error: Cannot read from DEVICE_READ, not currently connected

bluetooth.js

const onDeviceDisconnect = async (
  device,
  readSubscription,
  dispatch,
  changeDeviceConnectionStatus,
) => {
  readSubscription.remove()

  changeDeviceConnectionStatus('disconnected')
  dispatch(setDevice({ device: null }))

  console.log('disconnected device: ' + device.name)
}

export const sendMessageToDevice = async (device, msg) => {
  const msgStr = createBluetoothMessage(msg)

  try {
    await RNBluetoothClassic.writeToDevice(device.address, msgStr)
  } catch (error) {
    console.error('error reply message' + msgStr)
  }
}

export const connectBluetoothDevice = async (
  device,
  dispatch,
  changeDeviceConnectionStatus,
  onDataReceived,
) => {
  try {
    changeDeviceConnectionStatus('connecting')
    let connection = await device.isConnected()

    // check if already connected
    if (!connection) {
      connection = await device.connect({
        DELIMITER: Config.BLUETOOTH_DELIMITER,
      })
    }

    changeDeviceConnectionStatus('connected')

    let readSubscription = device.onDataReceived(data => onDataReceived(data))

    RNBluetoothClassic.onDeviceDisconnected(device =>
      onDeviceDisconnect(
        device,
        readSubscription,
        dispatch,
        changeDeviceConnectionStatus,
      ),
    )
  } catch (error) {
    console.error(error)
    changeDeviceConnectionStatus('connection failed')
    dispatch(setDevice({ device: null }))
  }
}

Screen.js

const ExampleContainer = () => {
  const device = useSelector(store => store.bluetooth.device)
  const [connectionStatus, setConnectionStatus] = useState('not connected')
  const dispatch = useDispatch()

  async function onDataReceived(data) {
    await data
    console.log(data)
  }

  async function onSendCommmand(command) {
    await sendMessageToDevice(device, { type: 'command', command: command })
  }

  useEffect(() => {
    async function connectDevice(device) {
      await connectBluetoothDevice(
        device,
        dispatch,
        setConnectionStatus,
        onDataReceived,
      )
    }

    if (device !== null) {
      connectDevice(device)
    }
  }, [device, dispatch])

  return (...)
  }
}

export default ExampleContainer

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
brunovollmercommented, Nov 3, 2021

So the problem was related to specific kind of message we send.

0reactions
kenjdavidsoncommented, Nov 3, 2021

Right on man, I’m glad you’ve got it up and going!

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 Ways to Read Subscription Based Newspaper Articles for Free
5 Ways to Read Subscription Based Newspaper Articles for Free · 1. Stop Loading the Page · 2. Delete the Page Cookies ·...
Read more >
The big problem with subscriptions - The Hustle
It's like when you forget to cancel a trial, so you get enrolled for a full month. Negative-option billing is fine if the...
Read more >
3 Reasons Subscription Services Fail
Subscribers pay an annual fee without knowing what articles they'll read in the latest editions of Time or Harvard Business Review. Many ...
Read more >
Fix problems with subscriptions - Android - Google Play Help
Depending on your issue, try the following fixes. Find missing subscriptions If you can't find your subscriptions, check that you're signed in to...
Read more >
How To Solve The Biggest Problems With Subscription Billing
How To Solve The Biggest Problems With Subscription Billing · Affordability and Conversions · Retention · Inefficient Payment Processes · Flexibility · Try ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found